将INTERVAL DAY转换为SECOND

时间:2018-01-25 13:31:34

标签: sql oracle

我希望使用区间数据类型获得更多可读值。

select cast(last_run_duration as INTERVAL DAY TO SECOND(0))
from dba_scheduler_jobs
order by 1 desc;

像这样查询结果;

+00 00:00:04.630530
+00 00:00:01.352359
+00 00:00:00.737282
+00 00:00:00.594381
+00 00:00:00.432710

我想要的是这个。只得到小时:分钟:秒;

 00:00:04
 00:00:01
 00:00:00
 00:00:00
 00:00:00

last_run_duration列的数据类型为INTERVAL DAY(9) TO SECOND(6)

2 个答案:

答案 0 :(得分:2)

尝试使用extract

select lpad(extract(hour from last_run_duration),2,'0')||':'||
       lpad(extract(minute from last_run_duration),2,'0')||':'||
       lpad(round(extract(second from last_run_duration)),2,'0') as duration
  from dba_scheduler_jobs
 order by 1 desc;

答案 1 :(得分:2)

INTERAVL DAY TO SECOND具有固定的输出格式,TO_CHAR不起作用。

使用Edumelzer提议的EXTRACT

select lpad(extract(hour from last_run_duration),2,'0')||':'||
       lpad(extract(minute from last_run_duration),2,'0')||':'||
       lpad(round(extract(second from last_run_duration)),2,'0') as duration
  from dba_scheduler_jobs
 order by 1 desc;

或使用像

这样的子字符串
select 
REGEXP_SUBSTR(last_run_duration, '\d{2}:\d{2}:\d{2}')
from from dba_scheduler_jobs;