oracle日期函数
一、常用日期数据格式
1. 获取年的最后一位, 两位, 三位, 四位
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
select to_char(sysdate,'Y') from dual; -- 获取年的最后一位
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
select to_char(sysdate,'YY') from dual; -- 获取年的最后两位
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
select to_char(sysdate,'YYY') from dual; -- 获取年的最后三位
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
select to_char(sysdate,'YYYY') from dual; -- 获取年的最后四位
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
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, [ ? ])
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
select sysdate S1, -- 返回当前日期,有时分秒
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
trunc(sysdate) S2, -- 返回当前日期,无时分秒
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
trunc(sysdate, 'year') YEAR, -- 返回当前年的1月1日,无时分秒
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
trunc(sysdate, 'month') MONTH, -- 返回当前月的1日,无时分秒
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
trunc(sysdate, 'day') DAY, -- 返回当前星期的星期天,无时分秒
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
trunc(sysdate, 'Q') QUARTER, -- 返回当前季度的1日,无时分秒
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
trunc(sysdate, 'D') WEEK -- 返回当前星期的星期天,无时分秒
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
2. round(d, [?]) 舍入到最接近的日期
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
select sysdate S1,
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
round(sysdate) S2,
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
round(sysdate, 'year') YEAR, -- 舍入到最接近的年 2014/1/1
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
round(sysdate, 'month') MONTH, -- 舍入到最接近的月 2014/6/1
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
round(sysdate, 'day') DAY -- 舍入到最接近的星期日 2014/5/18
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
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;
三、oracle日期时间函数实例
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. 获取某月中所有周五的具体日期
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
select to_char(b.a, 'YY-MM-DD')
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
from (select trunc(sysdate, 'mm') + rownum - 1 a
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
where rownum < 32) b
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
where to_char(b.a, 'day') = '星期五';
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
5. 查找2002-02-28至2002-02-01间除了星期一和七的天数
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
select count(*)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
from (select rownum - 1 row_num
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
from all_objects
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
where rownum <= to_date('2002-02-28', 'yyyy-mm-dd') -
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
to_date('2002-02-01', 'yyyy-mm-dd') + 1)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
![](http://www.studyofnet.com/Codefan-Controls/OutliningIndicators/None.gif)
where to_char(to_date('2002-02-01', 'yyyy-mm-dd') + row_num - 1, 'D') not in('1', '7')