當月數據
select * from table t
where t.create_time >=trunc(sysdate, 'MM') and t.create_time<=last_day(sysdate)
create_time爲你要查詢的時間
當年數據
select * from table t
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
本週(國內週一爲一個星期第一天)
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')
字段類型爲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())