Sql Server中查询当天,最近老三天,本周,本月,最近一个月,本季度的数量的sql语句

当天:

select * from T_news where datediff(day,addtime,getdate())=0

近日老三天:

select * from T_news where datediff(day,addtime,getdate())<= 2 and
datediff(day,addtime,getdate())>= 0

本周:

select * from T_news WHERE (DATEPART(wk, addtime) = DATEPART(wk,
GETDATE())) AND (DATEPART(yy, addtime) = DATEPART(yy, GETDATE()))

小心:此时无能够就此 datediff 差值为7,因为,datediff只象征间隔往往

本月:

select * from T_news WHERE (DATEPART(yy, addtime) = DATEPART(yy,
GETDATE())) AND (DATEPART(mm, addtime) = DATEPART(mm, GETDATE()))

近年一个月份:

select * from T_news WHERE (DATEPART(yy, addtime) = DATEPART(yy,
GETDATE())) 
AND ((DATEPART(mm, addtime) = DATEPART(mm, GETDATE())) OR
(31-DATEPART(DD,addtime)+DATEPART(DD, GETDATE()))<=31)

本季度:

select * from T_news where DATEPART(qq, addtime) = DATEPART(qq,
GETDATE()) and DATEPART(yy, addtime) = DATEPART(yy, GETDATE())

相关文章