sql語句獲取本週 本月 本年資料

2021-10-04 14:07:33 字數 1724 閱讀 3233

本週: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 

求以下日期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 時間字段 >= 時間字段

上月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

>>>更多

>>

sql datetime獲取本週,本月,本年

dateadd wk,datediff wk,0,getdate 1 2dateadd wk,datediff wk,0,getdate 6 34dateadd mm,datediff mm,0,getdate 0 5dateadd ms,3,dateadd mm,datediff m,0,getd...

sql查詢本週本月本年

1.乙個月第一天的 select dateadd mm,datediff mm,0,getdate 0 2.本周一 select dateadd wk,datediff wk,0,getdate 0 select dateadd wk,datediff wk,0,getdate 6 3.一年的第一天...

oracle SQL 語句取本週 本月 本年的資料

國內從周一到週日 國外是週日到週六 select to char sysdate 1,d from dual 取國內的星期幾 去掉減一取國外的星期 取本週時間內的資料 select from table where dtime trunc next day sysdate 8,1 1 and dti...