求日期Sql语句一条
table
date(varchar 6)
200612
200701
200702
200703
200704
........
我想查询我当前输入的月份的前3个月和后3个月
如果前6个月的记录只有2条,如200702 ,则显示数据库中存在的数据
Select * From (Select TOP 3 * From table Where date < @date
Order By dateDesc) A
Union All
Select * From table Where date= @date
Union All
Select * From (Select TOP 3 * From table Where date> @date
Order By date) B
我的在查询时应该显示200612时显示200601
高手指点一下
------解决方案--------------------declare @dt as datetime
set @dt = getdate()
select * from tb where abs(DATEDIFF(month , @dt , date)) <= 3
------解决方案----------------------查询我当前输入的月份的前3个月和后3个月
Select * From table Where date> dateadd(month,-3,@date) and date <=dateadd(month,3,@date) Order By dateDesc
--如果前6个月的记录只有2条,如200702 ,则显示数据库中存在的数据
if( @@rowcount <= 2)
select * from table
------解决方案--------------------declare @currentmonth char(6)
Select * From table Where abs(datediff(mm,convert(datetime,date+ '01 ',112),convert(datetime, @currentmonth+ '01 ',112))) <=3
--先进行一下日期转换就可以了