標籤:
當天:
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())
Sql Server中查詢當天,最近三天,本周,本月,最近一個月,本季度的資料的sql語句