今天閑來(lái)沒(méi)事, 特意從網(wǎng)上整理了一些資料, 以備日后查閱.
一、常用日期數(shù)據(jù)格式
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. 獲取當(dāng)前季度
select to_char(sysdate,'Q') from dual; -- 1 ~ 3月為第一季度, 2表示第二季度。
3. 獲取月份數(shù)
select to_char(sysdate,'MM') from dual; -- 五月為05
4. 獲取月份的羅馬表示
select to_char(sysdate,'RM') from dual; -- 五月為V
5. 獲取用9個(gè)字符長(zhǎng)度表示的月份名
select to_char(sysdate,'Month') from dual; -- 五月為5月
6. 獲取當(dāng)年第幾周
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. 獲取當(dāng)年第幾天
select to_char(sysdate,'DDD') from dual; -- 2014年5月20日為2014年第140天
9. 獲取當(dāng)月第幾天
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進(jìn)制小時(shí)數(shù)
select to_char(sysdate,'HH') from dual; -- 22:36分用12小時(shí)制計(jì)時(shí)為10點(diǎn)
13. 獲取24進(jìn)制小時(shí)數(shù)
select to_char(sysdate,'HH24') from dual; -- 22:36分用24小時(shí)制計(jì)時(shí)為22點(diǎn)
二、常用時(shí)間函數(shù)
1. trunc(d, [ ? ])
2. round(d, [?]) 舍入到最接近的日期
3. last_day(d) 獲取包含d的月最后一天的日期
select last_day(sysdate) from dual; -- 獲取本月最后一天: 2014/5/31 22:46:01
4. add_months(d, n) 日期d往后推n個(gè)月
select add_months(sysdate,2) from dual; -- 日期往后推2個(gè)月: 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間相差月數(shù)
select months_between(sysdate,to_date('2007-04-12','yyyy-mm-dd'))from dual; -- 85.2889874551971
7. 獲取兩個(gè)日期間的天數(shù)
select floor(sysdate - to_date('20140405','yyyymmdd')) from dual;
三、綜合用法
1. 獲取上個(gè)月最后一天
select to_char(add_months(last_day(sysdate),-1),'yyyy-MM-dd') lastDay from dual;
2. 獲取上個(gè)月的今天
select to_char(add_months(sysdate,-1),'yyyy-MM-dd') preToday from dual;
3. 獲取上個(gè)月的第一天
select to_char(add_months(last_day(sysdate)+1,-2),'yyyy-MM-dd') firstDay from dual;
4. 獲取某月中所有周五的具體日期
5. 查找2002-02-28至2002-02-01間除了星期一和七的天數(shù)
新聞熱點(diǎn)
疑難解答
圖片精選