当天
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 dbo.dt_article WHERE (DATEPART(yy, add_time) = DATEPART(yy, GETDATE())) AND (DATEPART(mm, add_time) = DATEPART(mm, GETDATE()))
最近一个月
select * from dbo.dt_article WHERE (DATEPART(yy, add_time) = DATEPART(yy, GETDATE()))
AND ((DATEPART(mm, addtime) = DATEPART(mm, GETDATE())) OR (31-DATEPART(DD, addtime)+DATEPART(DD, GETDATE()))<=31)
本季度
select * from dbo.dt_article where DATEPART(qq, add_time) = DATEPART(qq, GETDATE()) and DATEPART(yy, add_time) = DATEPART(yy, GETDATE())
本年度
select * from dbo.dt_article where datediff(year,add_time,getdate())=0
前一年
select * from dbo.dt_article where datediff(year,add_time,getdate())=1
备注:上面的sql语句中的add_time是表中的字段
dbo.dt_article 表
add_time 字段