select * from table where cdate >= convert(varchar(8), dateadd(month,-2,getdate()), 120)+'01' and cdate <= select convert(varchar(10), getdate(), 120) and ...
select * from table where ...table有字段cdate,其值形为"2007-08-01"条件是将当前时间所在月,和前一个月,后一个月,共3个月所有满足条件的都查处,这里的条件该怎么写?select * from table where abs(month(cast(cdate as datetime)) - month(getdate())) < = 1
select * from table where cdate between convert(varchar(7),dateadd(m,-1,getdate()),120)+'-01' and convert(varchar(7),dateadd(m,1,getdate()),120)+'-01'不知道可不可以
--不过最好还是不要对字段cdate进行转换,而是将条件转换成cdate的格式 select * from table where cdate >= convert(varchar(8), dateadd(month,-1,getdate()), 120)+'01' and cdate < convert(varchar(8), dateadd(month,2,getdate()), 120)+'01' and ...
select * from [table] where abs(month(cast(cdate as datetime)) - month(getdate())) < = 1 and year(cast(cdate as datetime))=year(getdate())
select * from table where ...table有字段cdate,其值形为"2007-08-01"条件是将当前时间所在月,和前一个月,后一个月,共3个月所有满足条件的都查处,这里的条件该怎么写?select * from table where abs(month(cast(cdate as datetime)) - month(getdate())) < = 1要考虑年啊. select * from table where abs(month(cast(cdate as datetime)) - month(getdate())) < = 1 and year(cdate) = year(getdate())
where cdate >= convert(varchar(8), dateadd(month,-2,getdate()), 120)+'01'
and cdate <= select convert(varchar(10), getdate(), 120)
and ...
select * from table
where cdate >= convert(varchar(8), dateadd(month,-1,getdate()), 120)+'01'
and cdate < convert(varchar(8), dateadd(month,2,getdate()), 120)+'01'
and ...
where abs(month(cast(cdate as datetime)) - month(getdate())) < = 1
and year(cast(cdate as datetime))=year(getdate())
select * from table where abs(month(cast(cdate as datetime)) - month(getdate())) < = 1 and year(cdate) = year(getdate())
应该符合楼主的要求了吧?