sql語句根據日期查詢,本周,本月,本年,今日相關統計


昨天
select * from tb where datediff(day, 時間字段 ,getdate()) = 1
今天
select * from tb where datediff(day, 時間字段 ,getdate()) = 0
本周
select * from tb where datediff(week, 時間字段 ,getdate()) = 0
上周
select * from tb where datediff(week, 時間字段 ,getdate()) = 1
下周
select * from tb where datediff(week, 時間字段 ,getdate()) = -1

上月
Select * From TableName Where DateDiff(month, 時間字段, GetDate()) = 1
本月
Select * From TableName Where DateDiff(month,時間字段, GetDate()) = 0
下月
Select * From TableName Where DateDiff(month, GetDate(), 時間字段) = -1
本年
Select * From TableName Where DateDiff(year, GetDate(), 時間字段 ) = 0

昨天:dateadd(day,-1,getdate())
明天:dateadd(day,1,getdate())
上月:month(dateadd(month, -1, getdate()))
本月:month(getdate())
下月:month(dateadd(month, 1, getdate()))

昨天 
Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) = 1
明天
Select * From TableName Where DateDiff(dd, GetDate(), DateTimCol) = -1
最近七天
Select * From TableName Where DateDiff(dd, DateTimCol, GetDate()) <= 7
隨后七天

當前年
select 提出日期, datepart(year,getdate()) as 當前年 from 供方資料表
前一年
select 提出日期, datepart(year,getdate())-1 as 當前年 from 供方資料表
后一年
select 提出日期, datepart(year,getdate())+1 as 當前年 from 供方資料表


免責聲明!

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



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