2

I have the following SQL Query:

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

This gives me the time elapsed between the two hours at which event1 and event2 happened in minutes.

My question: how do I enforce the time elapsed to be displayed not in minutes but in the following format HH24:MI:SS?

4

2 回答 2

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
于 2012-11-09T15:30:21.363 回答
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
于 2012-11-09T21:33:01.323 回答