タイムスタンプを加算または減算すると、結果は間隔
、別のタイムスタンプではありません。 抽出
> 機能
そこからコンポーネントを引き出します。値が常に1秒未満になる場合は、秒を抽出し、1000を掛けてミリ秒を取得できます。
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の代わりになります。
間隔が1秒を超える可能性がある場合は、おそらくミリ秒単位で値全体を取得する必要があるため、すべての要素を抽出して合計し、それらが表すものに基づいてそれぞれを乗算する必要があります。 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フィドル サンプルデータに基づいて、ある種の時間計算を逆にして、値が正になるようにします。