您的位置:首页 > 数据库 > Oracle

Oracle 获取本周、本月、本季度、本年等时间段

2013-04-22 20:29 351 查看
       
--本周
  select trunc(sysdate,'d')+1 from dual;
  select trunc(sysdate,'d')+7 from dual;
  --本月
  select trunc(sysdate,'mm') from dual;
  select last_day(trunc(sysdate)) from dual;
  --本季
  select trunc(sysdate,'Q') from dual;
  select add_months(trunc(sysdate,'Q'),3)-1 from dual;
  --本年
  select trunc(sysdate,'yyyy') from dual;
  select add_months(trunc(sysdate,'yyyy'),12)-1 from dual;
  -- 获取上月的开始时间和结束时间
  select to_char(to_date(to_char(add_months(sysdate,-1),'yyyy-mm'),'yyyy-mm'),'yyyy-mm-dd hh24:mi:ss') from dual;
  select to_char(to_date(to_char(sysdate,'yyyy-mm'),'yyyy-mm'),'yyyy-mm-dd hh24:mi:ss') from dual;
  -- 获取前一天的开始时间和结束时间
  select to_char(to_date(to_char(sysdate-1,'yyyy-mm-dd'),'yyyy-mm-dd'),'yyyy-mm-dd hh24:mi:ss') from dual;
  select to_char(to_date(to_char(sysdate,'yyyy-mm-dd'),'yyyy-mm-dd'),'yyyy-mm-dd hh24:mi:ss') from dual;
  -- 获取上一个小时的开始时间和结束时间
  select to_date(to_char(sysdate,'yyyy-mm-dd')||(to_char(sysdate,'hh24')-1),'yyyy-mm-dd hh24') from dual;
  select to_date(to_char(sysdate,'yyyy-mm-dd')||(to_char(sysdate,'hh24')),'yyyy-mm-dd hh24') from dual;

--当月数据

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;

转载自:
http://blog.sina.cn/dpool/blog/ArtRead.php?nid=685483d101018d7p http://tiantian0521.blog.163.com/blog/static/41720883201011153544240/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐