SQL 查詢過去時間內的數據


例如:

  今天的所有數據:select * from 表名 where DateDiff(dd,開始時間,getdate())=0

  昨天的所有數據:select * from 表名 where DateDiff(dd,開始時間,getdate())=1

  7天內的所有數據:select * from 表名 where DateDiff(dd,開始時間,getdate())<=7

  30天內的所有數據:select * from 表名 where DateDiff(dd,開始時間,getdate())<=30

  本月的所有數據:select * from 表名 where DateDiff(mm,開始時間,getdate())=0

  本年的所有數據:select * from 表名 where DateDiff(yy,開始時間,getdate())=0

 

DATEDIFF 函數

DATEDIFF(datepart,startdate,enddate)

startdate 和 enddate 參數是合法的日期表達式。

datepart 參數可以是下列的值:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM