更改两小时之间经过的时间的输出格式

时间:2012-11-09 15:08:20

标签: sql database oracle

我有以下SQL查询:

    SELECT 
    TO_CHAR(Event1, 'HH24:MI:SS'),
    TO_CHAR(Event2, 'HH24:MI:SS'),
    TO_CHAR((Event1-Event2) * -1440) AS Elapsed
...

这给了我在几分钟内发生event1和event2的两个小时之间的时间。

我的问题:如何强制显示的时间不是几分钟,而是采用以下格式HH24:MI:SS

2 个答案:

答案 0 :(得分:1)

您可以转换为TIMESTAMP,从而产生间隔数据类型

SQL> create table test(a date, b date);

Table created.

SQL> insert into test values (sysdate - 1.029384, sysdate);

1 row created.

SQL> select 1440*(b-a) diff_in_secs from test;

DIFF_IN_SECS
------------
  1482.31667

SQL> select (cast(b as timestamp)-cast(a as timestamp)) diff_in_secs from test;

DIFF_IN_SECS
---------------------------------------------------------------------------
+000000001 00:42:19.000000

您可以使用extract('hour' from your_interval_expression)等提取单个元素。

SQL> select extract(day from diff)||'d '||extract(hour from diff)||'h '||extract(minute from diff)||'m '||extract(second from diff)||'s' from (select (cast(b as timestamp)-cast
(a as timestamp)) diff from test);

EXTRACT(DAYFROMDIFF)||'D'||EX
--------------------------------------------------------------------------------
1d 0h 42m 19s

答案 1 :(得分:1)

尝试

select 
TRUNC(event1-event2)||':'||
TRUNC(MOD(event1-event2),1)*24)||':'||
TRUNC(MOD(MOD(event1-event2),1)*24,1)*60)||':'||
TRUNC(MOD(MOD(MOD((event1-event2),1)*24,1)*60,1)*60) as elapsed