När du lägger till eller subtraherar tidsstämplar är resultatet ett intervall
, inte en annan tidsstämpel. Du kan använda extract
funktion
att dra ut komponenterna ur det. Om ditt värde alltid kommer att vara undersekund kan du bara extrahera sekunderna och multiplicera med tusen för att få millisekunderna:
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
Här skulle din riktiga fråga ersätta den dummy CTE som jag använde med en bokstavlig intervall.
Om intervallet kan vara mer än en sekund så skulle du förmodligen vilja få hela värdet i millisekunder, så du måste extrahera alla element och lägga ihop dem, multiplicera var och en baserat på vad de representerar - så en hel dag skulle vara 86400000 millisekunder etc.; de vanliga elementen skulle komma ut 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
Och du skulle kombinera 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 baserat på din tidigare fråga kanske du vill ha den som en sträng, som separata 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-fiol baserat på dina exempeldata, typ och med tidsberäkningen omvänd så att värdet är positivt.