2014-12-08 BaoXinjian
一、摘要
INTERVAL數據類型用來存儲兩個時間戳之間的時間間隔。
可以指定years and months,或者days,hours,minuts,seconds之間的間隔。
ORACLE支持兩種INTEVAL類型,它們分別是YEAR TO MONTH和DAY TO SECOND。
每個類型都包含leading field和trailing field。主參數定義要被計算的date或者time,副參數定義最小增長量
二、語法
1. 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) ] } ]
(1). leading_precision值的范圍是0到9, 默認是2. time_expr的格式為:HH[:MI[:SS[.n]]] or MI[:SS[.n]] or SS[.n], n表示微秒.
(2). 該類型與INTERVAL YEAR TO MONTH有很多相似的地方,建議先看INTERVAL YEAR TO MONTH再看該文.
2. 范圍值:
HOUR: 0 to 23
MINUTE: 0 to 59
SECOND: 0 to 59.999999999
三、案例分析
1. INTERVAL '400 5' DAY(3) TO HOUR
表示: 400天5小時, 400為3為精度,所以"DAY(3)", 注意默認值為2
2. INTERVAL '30.12345' SECOND(2,4)
表示: 30.1235秒, 因為該地方秒的後面精度設置為4, 要進行四捨五入
3. 其他
select date '2010-01-01' + interval '21' year from dual
select date '2010-01-01' + interval '123-2' year(4) to month from dual
select date '2010-01-01' + interval '100' month from dual
select date '2010-01-01' + interval '100' day(2) from dual
select date '2010-01-01' + interval '1 2' day to hour from dual
select date '2010-01-01' + interval '1 2:3' day to minute from dual
select date '2010-01-01' + interval '1 2:3:4' day to second from dual
select date '2010-01-01' + interval '1' hour from dual
select date '2010-01-01' + interval '1:2' hour to minute from dual
select date '2010-01-01' + interval '1:2:3' hour to second from dual
select date '2010-01-01' + interval '1:2' minute to second from dual
Thanks and Regards