Sql Server中查询当天,前段时间四天,本周,上月,方今三个月,二〇二〇年的数量的sql语句

发布时间:2019-08-01  栏目:MySQL  评论:0 Comments

在意:此时不能用 datediff 差值为7,因为,datediff只象征间隔数

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

本季度:

select * from T_news where 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 (DATEPART(yy, addtime) = DATEPART(yy,
GETDATE())) AND (DATEPART(mm, addtime) = DATEPART(mm, GETDATE()))

当天:

本周:

select * from T_news where DATEPART(qq, addtime) = DATEPART(qq,
GETDATE()) and DATEPART(yy, addtime) = DATEPART(yy, 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)

留下评论

网站地图xml地图