ORACLE 當月、本週、當年SQL查詢

當月數據

select from table       

where t.create_time >=trunc(sysdate'MM')   and t.create_time<=last_day(sysdate   


create_time爲你要查詢的時間  

 

當年數據

select from table     

where t.create_time >=trunc(sysdate,'YYYY' 

and t.create_time<=add_months(trunc(sysdate,'YYYY'),12)-1  

 

本週(國外週日爲一個星期第一天)

where t.create_time >=trunc(sysdate,'day')+1 and t.create_time<=trunc(sysdate,'day')+6  

  

本週(國內週一爲一個星期第一天)

where t.create_time >=trunc(next_day(sysdate-8,1)+1and t.create_time<=trunc(next_day(sysdate-8,1)+7)+1 

當天 
to_date(ct.CREATE_DATE,'yyyy-mm-dd hh24:mi:ss') between
 to_date(to_char(sysdate,'yyyy-mm-dd') || ' 00:00:01','yyyy-mm-dd hh24:mi:ss') and
to_date(to_char(sysdate,'yyyy-mm-dd') || ' 23:59:59','yyyy-mm-dd hh24:mi:ss')


Oracle 
字段類型爲varchar2,格式要與格式化的樣式匹配 
當天 
select * from 表名 where to_char(to_date(字段名,'yyyy-mm-dd hh24:mi:ss'),'dd')=to_char(sysdate,'dd') 
當週 
select * from 表名 where to_char(to_date(字段名,'yyyy-mm-dd hh24:mi:ss'),'iw')=to_char(sysdate,'iw') 
當月 
select * from 表名 where to_char(to_date(字段名,'yyyy-mm-dd hh24:mi:ss'),'mm')=to_char(sysdate,'mm') 
當季度 
select * from 表名 where to_char(to_date(字段名,'yyyy-mm-dd hh24:mi:ss'),'q')=to_char(sysdate,'q') 

字段類型爲date 
當天 
select * from 表名 where to_char(字段名,'dd')=to_char(sysdate,'dd') 
當週 
select * from 表名 where to_char(字段名,'iw')=to_char(sysdate,'iw') 
當月 
select * from 表名 where to_char(字段名,'mm')=to_char(sysdate,'mm') 
當季度 
select * from 表名 where to_char(字段名,'q')=to_char(sysdate,'q') 


SQL 
當天 
select * from 表名 where DATEPART(dd,字段名) = DATEPART(dd, GETDATE()) and DATEPART(mm, 字段名) = DATEPART(mm, GETDATE()) and DATEPART(yy, 字段名) = DATEPART(yy, GETDATE()) 
當週 
select * from 表名 where DATEPART(wk, 字段名) = DATEPART(wk, GETDATE()) and DATEPART(yy, 字段名) = DATEPART(yy, GETDATE()) 
當月 
select * from 表名 where DATEPART(mm, 字段名) = DATEPART(mm, GETDATE()) and DATEPART(yy, 字段名) = DATEPART(yy, GETDATE()) 
當季度 
select * from 表名 where DATEPART(qq, 字段名) = DATEPART(qq, GETDATE()) and DATEPART(yy,字段名) = DATEPART(yy, GETDATE()) 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章