INTERVAL YEAR TO MONTH資料型別
Oracle語法: INTERVAL 'integer [- integer]' {YEAR | MONTH} [(precision)][TO {YEAR | MONTH}]
該資料型別常用來表示一段時間差, 注意時間差只精確到年和月. precision為年或月的精確域, 有效範圍是0到9, 預設值為2.
eg: INTERVAL '123-2' YEAR(3) TO MONTH 表示: 123年2個月, "YEAR(3)" 表示年的精度為3, 可見"123"剛好為3為有效數值, 如果該處YEAR(n), n<3就會出錯, 注意預設是2.
INTERVAL '123' YEAR(3) 表示: 123年0個月
INTERVAL '300' MONTH(3) 表示: 300個月, 注意該處MONTH的精度是3啊.
INTERVAL '4' YEAR 表示: 4年, 同 INTERVAL '4-0' YEAR TO MONTH 是一樣的
INTERVAL '50' MONTH 表示: 50個月, 同 INTERVAL '4-2' YEAR TO MONTH 是一樣
INTERVAL '123' YEAR 表示: 該處表示有錯誤, 123精度是3了, 但系統預設是2, 所以該處應該寫成 INTERVAL '123' YEAR(3) 或"3"改成大於3小於等於9的數值都可以的
INTERVAL '5-3' YEAR TO MONTH + INTERVAL '20' MONTH = INTERVAL '6-11' YEAR TO MONTH 表示: 5年3個月 + 20個月 = 6年11個月
與該型別相關的函式: NUMTODSINTERVAL(n, 'interval_unit') 將n轉換成interval_unit所指定的值, interval_unit可以為: DAY, HOUR, MINUTE, SECOND 注意該函式不可以轉換成YEAR和MONTH的.
NUMTOYMINTERVAL(n, 'interval_unit') interval_unit可以為: YEAR, MONTH
eg: (Oracle Version 9204, RedHat Linux 9.0) SQL> select numtodsinterval(100,'DAY') from dual;
NUMTODSINTERVAL(100,'DAY') --------------------------------------------------------------------------- +000000100 00:00:00.000000000
SQL> c/DAY/SECOND 1* select numtodsinterval(100,'SECOND') from dual SQL> /
NUMTODSINTERVAL(100,'SECOND') --------------------------------------------------------------------------- +000000000 00:01:40.000000000
SQL> c/SECOND/MINUTE 1* select numtodsinterval(100,'MINUTE') from dual SQL> /
NUMTODSINTERVAL(100,'MINUTE') --------------------------------------------------------------------------- +000000000 01:40:00.000000000
SQL> c/MINUTE/HOUR 1* select numtodsinterval(100,'HOUR') from dual SQL> /
NUMTODSINTERVAL(100,'HOUR') --------------------------------------------------------------------------- +000000004 04:00:00.000000000
SQL> c/HOUR/YEAR 1* select numtodsinterval(100,'YEAR') from dual SQL> / select numtodsinterval(100,'YEAR') from dual * ERROR at line 1: ORA-01760: illegal argument for function
SQL> select numtoyminterval(100,'year') from dual;
NUMTOYMINTERVAL(100,'YEAR') --------------------------------------------------------------------------- +000000100-00
SQL> c/year/month 1* select numtoyminterval(100,'month') from dual SQL> /
NUMTOYMINTERVAL(100,'MONTH') --------------------------------------------------------------------------- +000000008-04
時間的計算: SQL> select to_date('1999-12-12','yyyy-mm-dd') - to_date('1999-12-01','yyyy-mm-dd') from dual;
TO_DATE('1999-12-12','YYYY-MM-DD')-TO_DATE('1999-12-01','YYYY-MM-DD') --------------------------------------------------------------------- 11 -- 可以相減的結果為天.
SQL> c/1999-12-12/1999-01-12 1* select to_date('1999-01-12','yyyy-mm-dd') - to_date('1999-12-01','yyyy-mm-dd') from dual SQL> /
TO_DATE('1999-01-12','YYYY-MM-DD')-TO_DATE('1999-12-01','YYYY-MM-DD') --------------------------------------------------------------------- -323 -- 也可以為負數的
SQL> c/1999-01-12/2999-10-12 1* select to_date('2999-10-12','yyyy-mm-dd') - to_date('1999-12-01','yyyy-mm-dd') from dual SQL> /
TO_DATE('2999-10-12','YYYY-MM-DD')-TO_DATE('1999-12-01','YYYY-MM-DD') --------------------------------------------------------------------- 365193
下面看看INTERVAL YEAR TO MONTH怎麼用. SQL> create table bb(a date, b date, c interval year(9) to month);
Table created.
SQL> desc bb; Name Null? Type ----------------------------------------- -------- ---------------------------- A DATE B DATE C INTERVAL YEAR(9) TO MONTH
SQL> insert into bb values(to_date('1985-12-12', 'yyyy-mm-dd'), to_date('1984-12-01','yyyy-mm-dd'), null)
1 row created.
SQL> select * from bb;
A B --------- --------- C --------------------------------------------------------------------------- 12-DEC-85 01-DEC-84 SQL> update bb set c = numtoyminterval(a-b, 'year');
1 row updated.
SQL> select * from bb;
A B --------- --------- C --------------------------------------------------------------------------- 12-DEC-85 01-DEC-84 +000000376-00 -- 直接將相減的天變成年了, 因為我指定變成年的 SQL> select a-b, c from bb;
A-B ---------- C --------------------------------------------------------------------------- 376 +000000376-00
SQL> insert into bb values(null,null,numtoyminterval(376,'month'));
1 row created.
SQL> select * from bb;
A B C --------- --------- -------------------------------------------- 12-DEC-85 01-DEC-84 +000000376-00 +000000031-04
SQL> insert into bb values ( null,null, numtoyminterval(999999999,'year'));
1 row created.
SQL> select * from bb;
A B C --------- --------- --------------------------------------------------------------------- 12-DEC-85 01-DEC-84 +000000376-00 +000000031-04 +999999999-00
|