当前位置: 移动技术网 > IT编程>数据库>Oracle > oracle日期时间函数总结

oracle日期时间函数总结

2019年04月25日  | 移动技术网IT编程  | 我要评论

经常写 sql 的同学应该会接触到一些 oracle 的日期时间函数, 例如: 财务软件或者人力资源软件需要按照每年, 每季度, 每月, 甚至每个星期来进行统计.

今天闲来没事, 特意从网上整理了一些资料, 以备日后查阅.

一、常用日期数据格式

1. 获取年的最后一位, 两位, 三位, 四位

select to_char(sysdate,'y') from dual; -- 获取年的最后一位

select to_char(sysdate,'yy') from dual; -- 获取年的最后两位

select to_char(sysdate,'yyy') from dual; -- 获取年的最后三位

select to_char(sysdate,'yyyy') from dual; -- 获取年的最后四位

2. 获取当前季度

select to_char(sysdate,'q') from dual; -- 1 ~ 3月为第一季度, 2表示第二季度。

3. 获取月份数

select to_char(sysdate,'mm') from dual; -- 五月为05

4. 获取月份的罗马表示

select to_char(sysdate,'rm') from dual; -- 五月为v

5. 获取用9个字符长度表示的月份名

select to_char(sysdate,'month') from dual; -- 五月为5月

6. 获取当年第几周

select to_char(sysdate,'ww') from dual; -- 2014年5月20日为2014年第20周

7. 获取本月第几周

select to_char(sysdate,'w') from dual; -- 2014年5月20日为5月第3周

8. 获取当年第几天

select to_char(sysdate,'ddd') from dual; -- 2014年5月20日为2014年第140天

9. 获取当月第几天

select to_char(sysdate,'dd') from dual; -- 2014年5月20日为5月第20天

10. 获取一周第几天

select to_char(sysdate,'d') from dual; -- 2014年5月20日为一周第三天( 从周日算起 )

11. 获取中文的星期

select to_char(sysdate,'dy') from dual; -- 2014年5月20日为星期二

12. 获取12进制小时数

select to_char(sysdate,'hh') from dual; -- 22:36分用12小时制计时为10点

13. 获取24进制小时数

select to_char(sysdate,'hh24') from dual; -- 22:36分用24小时制计时为22点

二、常用时间函数

1. trunc(d, [ ? ])

select sysdate s1, -- 返回当前日期,有时分秒
       trunc(sysdate) s2, -- 返回当前日期,无时分秒
       trunc(sysdate, 'year') year, -- 返回当前年的1月1日,无时分秒
       trunc(sysdate, 'month') month, -- 返回当前月的1日,无时分秒
       trunc(sysdate, 'day') day, -- 返回当前星期的星期天,无时分秒
       trunc(sysdate, 'q') quarter, -- 返回当前季度的1日,无时分秒
       trunc(sysdate, 'd') week -- 返回当前星期的星期天,无时分秒
  from dual

2. round(d, [?]) 舍入到最接近的日期

select sysdate s1,
       round(sysdate) s2,
       round(sysdate, 'year') year, -- 舍入到最接近的年 2014/1/1
       round(sysdate, 'month') month, -- 舍入到最接近的月 2014/6/1
       round(sysdate, 'day') day -- 舍入到最接近的星期日 2014/5/18
  from dual

3. last_day(d) 获取包含d的月最后一天的日期

select last_day(sysdate) from dual; -- 获取本月最后一天: 2014/5/31 22:46:01

4. add_months(d, n) 日期d往后推n个月

select add_months(sysdate,2) from dual; -- 日期往后推2个月: 2014/7/20 22:49:36

5. next_day(d, day)

select next_day(sysdate,2) from dual; -- 日期sysdate之后的第一周中, 指定星期的第2天是什么日期

6. months_between(f,s) 日期f和s间相差月数

select months_between(sysdate,to_date('2007-04-12','yyyy-mm-dd'))from dual; -- 85.2889874551971

7. 获取两个日期间的天数

select floor(sysdate - to_date('20140405','yyyymmdd')) from dual;

三、综合用法

1. 获取上个月最后一天

select to_char(add_months(last_day(sysdate),-1),'yyyy-mm-dd') lastday from dual;

2. 获取上个月的今天

select to_char(add_months(sysdate,-1),'yyyy-mm-dd') pretoday from dual;

3. 获取上个月的第一天

select to_char(add_months(last_day(sysdate)+1,-2),'yyyy-mm-dd') firstday from dual;

4. 获取某月中所有周五的具体日期

select to_char(b.a, 'yy-mm-dd')
  from (select trunc(sysdate, 'mm') + rownum - 1 a
          from dba_objects
         where rownum < 32) b
 where to_char(b.a, 'day') = '星期五';

5. 查找2002-02-28至2002-02-01间除了星期一和七的天数

select count(*)
  from (select rownum - 1 row_num
          from all_objects
         where rownum <= to_date('2002-02-28', 'yyyy-mm-dd') -
               to_date('2002-02-01', 'yyyy-mm-dd') + 1)
 where to_char(to_date('2002-02-01', 'yyyy-mm-dd') + row_num - 1, 'd') not in('1', '7'

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网