oracle 日期時(shí)間函數(shù)使用總結(jié)
今天閑來(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, [ ? ])
select sysdate S1, -- 返回當(dāng)前日期,有時(shí)分秒
trunc(sysdate) S2, -- 返回當(dāng)前日期,無(wú)時(shí)分秒
trunc(sysdate, 'year') YEAR, -- 返回當(dāng)前年的1月1日,無(wú)時(shí)分秒
trunc(sysdate, 'month') MONTH, -- 返回當(dāng)前月的1日,無(wú)時(shí)分秒
trunc(sysdate, 'day') DAY, -- 返回當(dāng)前星期的星期天,無(wú)時(shí)分秒
trunc(sysdate, 'Q') QUARTER, -- 返回當(dāng)前季度的1日,無(wú)時(shí)分秒
trunc(sysdate, 'D') WEEK -- 返回當(dāng)前星期的星期天,無(wú)時(shí)分秒
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個(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. 獲取某月中所有周五的具體日期
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間除了星期一和七的天數(shù)
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'
相關(guān)文章
連接Oracle數(shù)據(jù)庫(kù)失敗(ORA-12514)故障排除全過(guò)程
Oracle連接失敗是指在使用Oracle數(shù)據(jù)庫(kù)進(jìn)行開發(fā)的過(guò)程中,服務(wù)器端無(wú)法與客戶端連接,從而導(dǎo)致Oracle連接無(wú)法成功,影響開發(fā)的效率,下面這篇文章主要給大家介紹了關(guān)于連接Oracle數(shù)據(jù)庫(kù)失敗(ORA-12514)故障排除的相關(guān)資料,需要的朋友可以參考下2023-05-05oracle 數(shù)據(jù)庫(kù)數(shù)據(jù)遷移解決方案
大部分系統(tǒng)由于平臺(tái)和版本的原因,做的是邏輯遷移,少部分做的是物理遷移,接下來(lái)把心得與大家分享一下2012-12-12Oracle Arraysize設(shè)置對(duì)于邏輯讀的影響實(shí)例分析
這篇文章主要介紹了Oracle Arraysize設(shè)置對(duì)于邏輯讀的影響實(shí)例分析,通過(guò)設(shè)置Arraysize大幅減少了邏輯讀的次數(shù)和網(wǎng)絡(luò)往返次數(shù),需要的朋友可以參考下2014-07-07Oracle9i數(shù)據(jù)庫(kù)異常關(guān)閉后的啟動(dòng)
Oracle9i數(shù)據(jù)庫(kù)異常關(guān)閉后的啟動(dòng)...2007-03-03Oracle獲取執(zhí)行計(jì)劃的六種方法總結(jié)
執(zhí)行計(jì)劃(explain plan)是指一條查詢語(yǔ)句在數(shù)據(jù)庫(kù)中的執(zhí)行過(guò)程或訪問(wèn)路徑的描述,下面這篇文章主要給大家總結(jié)介紹了關(guān)于Oracle獲取執(zhí)行計(jì)劃的六種方法,需要的朋友可以參考下2024-01-01