您好,欢迎访问一九零五行业门户网

MSSQL获取昨天,本周,本月 sqlserver时间获取

特别说明下:以下统计本周数据时,星期天是作为下周的第一天,而不是本周最后一天,因此你把星期天作为本周最后一天时,你需要在getdate()的基础上减一天,如dateadd('day', -1, getdate()) 本周:select * from table where datediff(week,c_calltime,getdat
特别说明下:以下统计本周数据时,星期天是作为下周的第一天,而不是本周最后一天,因此你把星期天作为本周最后一天时,你需要在getdate()的基础上减一天,如dateadd('day', -1, getdate())
本周: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
昨天
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和dateadd函数结合使用就可以了。
用datediff(day,时间列,getdate())
上月
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())
--随后七天
select * from tablename where datediff(dd, getdate(), datetimcol)
--上周
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
--------------------------------------------------------
1.现在我需要得到只是日期部分,时间部分不要,sql怎么写?
select convert(varchar(10),getdate(),120)
2.求以下日期sql:
昨天
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 时间字段 时间字段 >= 时间字段
其它类似信息

推荐信息