SQL獲取本周、本月、本季度的記錄的語句


前提條件:假設表名為:tableName;時間字段名為:theDate

  ①查詢本周的記錄

  select * from tableName where DATEPART(wk, theDate) = DATEPART(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())

  ②查詢本月的記錄

  select * from tableName where DATEPART(mm, theDate) = DATEPART(mm, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())

  ③查詢本季的記錄

  select * from tableName where DATEPART(qq, theDate) = DATEPART(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())

  select datediff(day,dateadd(mm, datediff(mm,'''',getdate()), ''''),dateadd(mm, datediff(mm,'''',getdate()), ''1900-02-01''))

  ④查詢本月記錄的通用語句

  SELECT * FROM [表名] WHERE (DATEPART(mm, 日期) = @month) AND (DATEPART(yy, 日期) = @year)

 

 

本周:select * from table where datediff(week,C_CALLTIME,getdate())=0     --C_CALLTIME 為日期字段
本月:select * from table where datediff(Month,C_CALLTIME,getdate())=0   --C_CALLTIME 為日期字段
本季:select * from table where datediff(qq,C_CALLTIME,getdate())=0 
前半年1-6,后半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7


免責聲明!

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



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