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

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐