原文連接地址:http://blog.knowsky.com/186111.htm函數
本週:select * from table where datediff(week,C_CALLTIME,getdate())=0 --C_CALLTIME 爲日期字段spa
本月:select * from table where datediff(Month,C_CALLTIME,getdate())=0 --C_CALLTIME 爲日期字段htm
本季:select * from table where datediff(qq,C_CALLTIME,getdate())=0blog
前半年1-6,後半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7get
昨天 table
select convert(varchar(10),getdate() - 1,120)基礎
明天date
select convert(varchar(10),getdate() + 1,120)select
最近七天im
select * from tb where 時間字段 >= convert(varchar(10),getdate() - 7,120)
隨後七天
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
--明天
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
--------------------------------------------------------
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 時間字段 <= convert(varchar(10),getdate() + 7,120) and 時間字段 >= 時間字段