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

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图