Sql Server中查询当天,近期二十日,本周,上月,方今三个月,前一季度的数码的sql语句

作者:美高梅mgm59599

美高梅手机登录网站www.mgm59599.com美高梅mgm59599, 

--当天:

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

本文由美高梅mgm59599发布,转载请注明来源

关键词: