大家请看这句SQL, select * from kbs where DATEPART(wk, addtime) = DATEPART(wk, GETDATE()) and DATEPART(yy, addtime) = DATEPART(yy, GETDATE()) order by convert(datetime,addtime) Desc 此句是查询一周的信息,其中addtime存在数据库中是varchar类型,但是运行时报错,提示将字符串转换为datetime 数据类型时失败。 同样,这句SQL也是这个问题 select * from kbs where convert(datetime,addTime) between '2009-9-2' and '2009-9-30' order by convert(datetime,addtime) Desc 请问应该怎么解决啊!
where addTime <'2009-9-2' and addTime >'2009-9-30'看看数据是不是有 异常
下面代码好像没出错啊,楼主可能数据长度大小出问题了
if object_id('tb') is not null
drop table tb
go
create table tb(id int identity(1,1),addTime varchar(25) )
insert tb(addTime)
select '2009-9-3' union all
select '2009-9-12' union all
select '2009-9-13' union all
select '2009-9-15' union all
select '2009-9-17' union all
select '2009-9-29'select * from tb where convert(datetime,addTime) between '2009-9-2'
and '2009-9-30' order by convert(datetime,addtime) Desc
where DATEPART(wk, convert(datetime,addtime)) = DATEPART(wk, GETDATE())
and DATEPART(yy, convert(datetime,addtime)) = DATEPART(yy, GETDATE())
order by convert(datetime,addtime) Desc