declare @t table
(dt datetime)insert @t
select getdate() union all
select dateadd(hour,10,getdate()) union all
select dateadd(hour,12,getdate()) union all
select dateadd(hour,-9,getdate()) union all
select dateadd(hour,20,getdate()) union all
select dateadd(hour,-20,getdate())select * from @t
where abs(datediff(hour,dt,getdate()))<=12
dt
------------------------------------------------------
2006-01-11 18:32:58.107
2006-01-12 04:32:58.107
2006-01-12 06:32:58.107
2006-01-11 09:32:58.107(所影响的行数为 4 行)
(dt datetime)insert @t
select getdate() union all
select dateadd(hour,10,getdate()) union all
select dateadd(hour,12,getdate()) union all
select dateadd(hour,-9,getdate()) union all
select dateadd(hour,20,getdate()) union all
select dateadd(hour,-20,getdate())select * from @t
where abs(datediff(hour,dt,getdate()))<=12
dt
------------------------------------------------------
2006-01-11 18:32:58.107
2006-01-12 04:32:58.107
2006-01-12 06:32:58.107
2006-01-11 09:32:58.107(所影响的行数为 4 行)
select id, tit, times from 表 where datediff(day,times,getdate())=02、距离当前时间不超过24小时的数据
declare @date datetime
select @date=getdate()
select id, tit, times from 表 where times between dateadd(day,-1,@date) and dateadd(day,1,@date)
substring(时间,1,4)=2004
group by substring(时间,5,2)时间格式: 20041229210100结果数据:259 01
163 02
131 03
143 04
146 05
118 06
160 07
116 08
96 09
110 10
92 11
103 12