SQLServer查询最近一天,本季度的数据的sql语句

当天:

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

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()))*

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

注意:此时不能用 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(wk, addtime) = DATEPART(wk, GETDATE())) AND (DATEPART(yy, addtime) = DATEPART(yy, GETDATE()))

一季度 select * from T_news where DATEPART(qq, addtime) = DATEPART(qq, 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())

本文由星彩网app下载发布于星彩彩票app下载,转载请注明出处:SQLServer查询最近一天,本季度的数据的sql语句

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。