Saat Anda menambah atau mengurangi stempel waktu, hasilnya adalah interval
, bukan stempel waktu lainnya. Anda dapat menggunakan extract
fungsi
untuk menarik komponen keluar dari itu. Jika nilai Anda akan selalu menjadi sub-detik, Anda cukup mengekstrak detik, dan mengalikannya dengan seribu untuk mendapatkan milidetik:
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
Di sini kueri Anda yang sebenarnya akan menggantikan CTE dummy yang saya gunakan dengan literal interval.
Jika intervalnya mungkin lebih dari satu detik maka Anda mungkin ingin mendapatkan seluruh nilai dalam milidetik, jadi Anda perlu mengekstrak semua elemen dan menambahkannya bersama-sama, mengalikan masing-masing berdasarkan apa yang mereka wakili - jadi sehari penuh akan menjadi 8640000 milidetik dll .; elemen polos akan keluar seperti:
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
Dan Anda akan menggabungkannya seperti:
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
Tetapi berdasarkan pertanyaan Anda sebelumnya, mungkin Anda menginginkannya sebagai string, sebagai komponen terpisah:
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 berdasarkan data sampel Anda, semacamnya, dan dengan penghitungan waktu terbalik sehingga nilainya positif.