微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

特别说明下:以下统计本周数据时,星期天是作为下周的第一天,而不是本周最后一天,因此你把星期天作为本周最后一天时,你需要在getDate()的基础上减一天,如dateadd('day',-1,getDate())

本周:select * from table where datediff(week,C_CALLTIME,getdate())=0 --C_CALLTIME 为日期字段

本月:select * from table where datediff(Month,宋体"> 本季:select * from table where datediff(qq,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 * from tb where 时间字段 >= convert(varchar(10),getdate() - 7,宋体"> 随后七天

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

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

--明天

ottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; font-size:13px; color:rgb(51,GetDate(),DateTimCol) = 1

--最近七天

ottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; font-size:13px; color:rgb(51,GetDate()) <= 7

--随后七天

ottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; font-size:13px; color:rgb(51,DateTimCol) <= 7

--上周

Select * From TableName Where DateDiff(wk,宋体"> --本周

ottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; font-size:13px; color:rgb(51,GetDate()) = 0

--下周

ottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; font-size:13px; color:rgb(51,DateTimCol ) = 1

--上月

Select * From TableName Where DateDiff(mm,宋体"> --本月

--下月

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

本周

select * from tb where datediff(week,时间字段,getdate()) = 0

上周

ottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; font-size:13px; color:rgb(51,getdate()) = 1

下周

ottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; font-size:13px; color:rgb(51,getdate()) = -1

1.现在我需要得到只是日期部分,时间部分不要,sql怎么写?

2.求以下日期sql:

ottom:0px; margin-top:8px; margin-bottom:8px; line-height:23px; font-size:13px; color:rgb(51,120) and 时间字段 >= 时间字段

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐