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

       
 --本周
  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/


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值