當您添加或減去時間戳,the result is an interval,不是另一個時間戳。您可以使用the extract
function將組件拉出。如果你的價值始終會是一秒,你可以只提取秒,由一千乘以獲得毫秒:
with t as (
select 1 as msg_guid,
interval '0 0:0:0.343009' day to second as diff
from dual
)
select trunc(extract (second from diff) * 1000)
from t;
TRUNC(EXTRACT(SECONDFROMDIFF)*1000)
-----------------------------------
343
這裏你真正的查詢會採取假CTE的我所使用的地方間隔文字。
如果間隔可能會超過一秒鐘,那麼你大概會想以毫秒爲單位的整數值,所以你需要解壓的所有元素,並把它們相加,乘以每個基礎上,他們代表了什麼 - 所以一整天的時間是86400000毫秒等等。純元素會出來這樣的:
column diff format a25
with t as (
select 1 as msg_guid,
systimestamp - trunc(systimestamp) as diff
from dual
)
select diff,
extract (day from diff) as dd,
extract (hour from diff) as hh,
extract (minute from diff) as mi,
extract (second from diff) as ss
from t;
DIFF DD HH MI SS
---------------------- ---------- ---------- ---------- ----------
0 9:13:26.150627 0 9 13 26.150627
而且你將它們組合,如:根據您剛纔的問題
with t as (
select 1 as msg_guid,
systimestamp - trunc(systimestamp) as diff
from dual
)
select diff,
trunc(1000 * (
extract (day from diff) * (60*60*24)
+ extract (hour from diff) * (60*60)
+ extract (minute from diff) * 60
+ extract (second from diff)
)) as milliseconds
from t;
DIFF MILLISECONDS
---------------------- ------------
0 9:13:27.650365 33207650
但是,也許你想作爲一個字符串,作爲獨立的部分組成:
with t as (
select 1 as msg_guid,
systimestamp - trunc(systimestamp) as diff
from dual
)
select diff,
extract (day from diff) || ' DAYS '
|| extract (hour from diff) || ' HOURS '
|| extract (minute from diff) || ' MINUTES '
|| trunc(extract (second from diff)) || ' SECONDS '
|| (trunc(extract (second from diff) * 1000)
- (trunc(extract (second from diff)) * 1000)) || ' MILLISECONDS'
as text
from t;
DIFF TEXT
---------------------- -------------------------------------------------------
0 9:43:38.896007 0 DAYS 9 HOURS 43 MINUTES 38 SECONDS 896 MILLISECONDS
SQL Fiddle根據您的樣本數據,排序,並用時間計算逆轉,因此該值是正的。
列的值有兩個日期的差異,使用http://stackoverflow.com/questions/23426364/write-oracle-db-view-based-on-different-column-time-stamp-區別 – kumarb