Når du tilføjer eller trækker tidsstempler fra, er resultatet et interval
, ikke et andet tidsstempel. Du kan bruge extract
funktion
at trække komponenterne ud af det. Hvis din værdi altid vil være sub-sekund, kan du bare udtrække sekunderne og gange med tusind for at få millisekunderne:
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
Her ville din rigtige forespørgsel træde i stedet for den dummy CTE, jeg brugte med et interval literal.
Hvis intervallet kan være mere end et sekund, vil du formentlig ønske at få hele værdien i millisekunder, så du bliver nødt til at udtrække alle elementerne og lægge dem sammen, multiplicere hver baseret på, hvad de repræsenterer - så en hel dag ville være 86400000 millisekunder osv.; de almindelige elementer ville komme ud som:
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
Og du ville kombinere dem som:
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
Men baseret på dit tidligere spørgsmål, vil du måske have det som en streng, som de separate komponenter:
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 baseret på dine prøvedata, slags og med tidsberegningen omvendt, så værdien er positiv.