程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle數據庫基礎 >> Oracle中的INTERVAL數據類型詳解

Oracle中的INTERVAL數據類型詳解

編輯:Oracle數據庫基礎

‍‍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                                                                

========================
今天來添加點新的東西![2008-07-26] 這部分東東來源:http://www.Oraclefans.cn/forum/showblog.JSP?rootid=139

INTERVAL YEAR TO MONTH類型2個TIMESTAMP類型的時間差別。內部類型是182,長度是5。其中4個字節存儲年份差異,存儲的時候在差異上加了一個0X80000000的偏移量。一個字節存儲月份的差異,這個差異加了60的偏移量。

SQL> ALTER TABLE TestTimeStamp ADD E INTERVAL YEAR TO MONTH;
SQL> update testTimeStamp set e=(select interval '5' year + interval '10' month year from dual);

已更新3行。

SQL> commit;
提交完成。

SQL> select dump(e,16) from testTimeStamp;

DUMP(E,16)
---------------------------------------------
Typ=182 Len=5: 80,0,0,5,46
Typ=182 Len=5: 80,0,0,5,46
Typ=182 Len=5: 80,0,0,5,46

年:0X80000005-0X80000000=5
月:0x46-60=10

INTERVAL DAY TO SECOND數據類型

Oracle語法:
INTERVAL '{ integer | integer time_expr | time_expr }'
{ { DAY | HOUR | MINUTE } [ ( leading_precision ) ]
| SECOND [ ( leading_precision [, fractional_seconds_precision ] ) ] }
[ TO { DAY | HOUR | MINUTE | SECOND [ (fractional_seconds_precision) ] } ]

leading_precision值的范圍是0到9, 默認是2. time_expr的格式為:HH[:MI[:SS[.n]]] or MI[:SS[.n]] or SS[.n], n表示微秒.
該類型與INTERVAL YEAR TO MONTH有很多相似的地方,建議先看INTERVAL YEAR TO MONTH再看該文.

范圍值:
HOUR:    0 to 23
MINUTE: 0 to 59
SECOND: 0 to 59.999999999

eg:
INTERVAL '4 5:12:10.222' DAY TO SECOND(3)
表示: 4天5小時12分10.222秒

INTERVAL '4 5:12' DAY TO MINUTE
表示: 4天5小時12分

INTERVAL '400 5' DAY(3) TO HOUR
表示: 400天5小時, 400為3為精度,所以"DAY(3)", 注意默認值為2.

INTERVAL '400' DAY(3)
表示: 400天

INTERVAL '11:12:10.2222222' HOUR TO SECOND(7)
表示: 11小時12分10.2222222秒

INTERVAL '11:20' HOUR TO MINUTE
表示: 11小時20分

INTERVAL '10' HOUR
表示: 10小時

INTERVAL '10:22' MINUTE TO SECOND
表示: 10分22秒

INTERVAL '10' MINUTE
表示: 10分

INTERVAL '4' DAY
表示: 4天

INTERVAL '25' HOUR
表示: 25小時

INTERVAL '40' MINUTE
表示: 40分

INTERVAL '120' HOUR(3)
表示: 120小時

INTERVAL '30.12345' SECOND(2,4)    
表示: 30.1235秒, 因為該地方秒的後面精度設置為4, 要進行四捨五入.

INTERVAL '20' DAY - INTERVAL '240' HOUR = INTERVAL '10-0' DAY TO SECOND
表示: 20天 - 240小時 = 10天0秒

==================
該部分來源:http://www.Oraclefans.cn/forum/showblog.JSP?rootid=140
INTERVAL DAY TO SECOND類型存儲兩個TIMESTAMP之間的時間差異,用日期、小時、分鐘、秒鐘形式表示。該數據類型的內部代碼是183,長度位11字節:

l         4個字節表示天數(增加0X80000000偏移量)
l         小時、分鐘、秒鐘各用一個字節表示(增加60偏移量)
l         4個字節表示秒鐘的小時差異(增加0X80000000偏移量)

以下是一個例子:

SQL> alter table testTimeStamp add f interval day to second ;

表已更改。

SQL> update testTimeStamp set f=(select interval '5' day + interval '10' second from dual);

已更新3行。

SQL> commit;

提交完成。

SQL> select dump(f,16) from testTimeStamp;

DUMP(F,16)

--------------------------------------------------------------------------------

Typ=183 Len=11: 80,0,0,5,3c,3c,46,80,0,0,0
Typ=183 Len=11: 80,0,0,5,3c,3c,46,80,0,0,0
Typ=183 Len=11: 80,0,0,5,3c,3c,46,80,0,0,0

日期:0X80000005-0X80000000=5

小時:60-60=0
分鐘:60-60=0
秒鐘:70-60=10
秒鐘小數部分:0X80000000-0X80000000=0

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved