SQLServer查詢最近一天,三天,一週,一月,一季度方法

iSQlServer發表於2010-08-31

一天 "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()))";

一月"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())";

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-672234/,如需轉載,請註明出處,否則將追究法律責任。

相關文章