SqlServer 查詢最近一段范圍中的數據


sql得到當前系統時間得 日期部分

CONVERT(varchar(10),getDate(),120)

 

昨天

select convert(varchar(10),getdate() - 1,120)

明天

select convert(varchar(10),getdate() + 1,120)

最近七天

select * from tb where 時間字段 >= convert(varchar(10),getdate() - 7,120)

隨后七天

select * from tb where 時間字段 <= convert(varchar(10),getdate() + 7,120) and 時間字段 >= 時間字段

 

上月

select * from tb where month(時間字段) = month(getdate()) - 1

本月

select * from tb where month(時間字段) = month(getdate())

下月

select * from tb where month(時間字段) = month(getdate()) + 1

--昨天

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 * From TableName Where DateDiff(dd, GetDate(), DateTimCol) <= 7

--上周

Select * From TableName Where DateDiff(wk, DateTimCol, GetDate()) = 1

--本周

Select * From TableName Where DateDiff(wk, DateTimCol, GetDate()) = 0

--下周

Select * From TableName Where DateDiff(wk, GetDate(), DateTimCol ) = 1

--上月

Select * From TableName Where DateDiff(mm, DateTimCol, GetDate()) = 1

--本月

Select * From TableName Where DateDiff(mm, DateTimCol, GetDate()) = 0

--下月

Select * From TableName Where DateDiff(mm, GetDate(), DateTimCol ) = 1

--------------------------------------------------------

本周

select * from tb where datediff(week , 時間字段 ,getdate()) = 0

上周

select * from tb where datediff(week , 時間字段 ,getdate()) = 1

下周

select * from tb where datediff(week , 時間字段 ,getdate()) = -1

 

純數字時間這樣轉換

and datediff(mm,GETDATE(),convert(DATETIME,CONVERT(NVARCHAR,
substring(CONVERT(varchar(100),YL01),0,9),20))) <=3


免責聲明!

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



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