oracle 当前年 当前月 当前周 当前日
Oracle 当前月 当前周 当前年 2010-12-15 15:54:04| 分类: 数据库 -- oracle | 标签:t.c reate_time sysdate trunc where |字号大中小 订阅 .
当月数据
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 本周(国内周一为一个星期第一天)
where t.create_time >=trunc(next_day(sysdate-8,1)+1) and t.create_time<=trunc(next_day(sysdate-8,1)+7)
select
trunc(next_day(sysdate - 8, 1) + 1) as 周一,
trunc(next_day(sysdate - 8, 1) + 2) as 周二,
trunc(next_day(sysdate - 8, 1) + 3) as 周三,
trunc(next_day(sysdate - 8, 1) + 4) as 周四,
trunc(next_day(sysdate - 8, 1) + 5) as 周五,
trunc(next_day(sysdate - 8, 1) + 6) as 周六,
trunc(next_day(sysdate - 8, 1) + 7) as 周日
from dual;
-----查询当期日期之前一个月的日期
select trunc(sysdate) - rownum +1 as tdate
from all_objects
where rownum <= (trunc(sysdate) - add_months(trunc(sysdate),-1 ))
-----当日9:00--21:00
select * from A where to_char(sysdate,'hh24:mi') between '09:00' and '21:00' where to_char(A.time,'yyyy-MM-dd)=to_char(sysdate,'yyyy-MM-dd);
-----oracle SQL语句中查询一个月内固定时间段的数据,比如9月1号到10月1号每天的八点到九点的呼叫数目
select * from table where createdate
between to_date('2010-9-1','yyyy-MM-dd') and to_date('2010-10-1','yyyy-MM-dd')
and EXTRACT(hour from createdate) between '8:00' and '9:00';
select a.日期,count(*) from 表名 a
where to_char(a.具体时间,'HH') <= '21' and to_char(a.具体时间,'HH')>='20'
group by a.日期
having to_char(a.日期,'YYYY-MM-DD')<='2011-10-01' and to_char(a.日期,'YYYY-MM-DD')>='2011-09-01'
-----当前月的天数
select trunc(sysdate) - rownum +1 as tdate
from all_objects
where rownum <= (trunc(sysdate)+1 - trunc(last_day(add_months(sysdate,-1))+1))
-----当月里的第一天,最后一天,总数
select (trunc(last_day(SYSDATE)) + 1 -
add_months(trunc(last_day(add_months(sysdate, -1)) + 1), 0)) totalday,
sysdate,
add_months(sysdate, 0) aa,
last_day(SYSDATE) lastday,
last_day(add_months(sysdate, -1)) + 1 firstday
from dual;