Sql Server中询问当天,近日三天,本周,本月,近来一个月,本季度的数量的sql语句

发布时间:2019-02-09  栏目:MyBatis  评论:0 Comments

当天:

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

select * from T_manbet手机客户端3.0,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(yy, addtime) =
DATEPART(yy, GETDATE())) 
AND ((DATEPART(mm, addtime) = DATEPART(mm, GETDATE())) OR
(31-DATEPART(DD,addtime)+DATEPART(DD, GETDATE()))<=31)

小心:此时不可能用 datediff 差值为7,因为,datediff只象征间隔数

本季度 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()))

前不久一个月:

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地图