Oracle:计算两个日期之间HH:MM:SS的时差

时间:2014-01-01 09:25:24

标签: oracle date

我有两个日期格式如下:

ST_DT =太阳12月29日11:55:29 2013

ED_DT = Tue Dec 30 20:21:34 EST 2013

我想找到HH:MM:SS格式中这两个日期之间的区别。现在我的问题是我不知道如何在Oracle中解析上述日期格式。

3 个答案:

答案 0 :(得分:1)

varchar2中的日期是否类型?然后,您可以先将其转换为时间戳格式。由于它也有时区,请使用to_timestamp_tz函数。

SQL> select to_timestamp_tz('Sun Dec 29 11:55:29 EST 2013','Dy Mon dd hh24:mi:ss TZR yyyy') from dual;

TO_TIMESTAMP_TZ('SUNDEC2911:55:29EST2013','DYMONDDHH24:MI:SSTZRYYYY')
---------------------------------------------------------------------------
29-DEC-13 11.55.29.000000000 AM EST

一旦日期采用时间戳类型,减去它们将为您提供interval day to second类型的差异。

SQL> select   to_timestamp_tz ('Mon Dec 30 20:21:34 EST 2013','Dy Mon dd hh24:mi:ss TZR yyyy')
  2         - to_timestamp_tz ('Sun Dec 29 11:55:29 EST 2013','Dy Mon dd hh24:mi:ss TZR yyyy') from dual;

TO_TIMESTAMP_TZ('MONDEC3020:21:34EST2013','DYMONDDHH24:MI:SSTZRYYYY')-TO_TI
---------------------------------------------------------------------------
+000000001 08:26:05.000000000

然后使用extract从间隔中获取单个组件。

SQL> select extract(day from intrvl) as dd,
  2         extract(hour from intrvl) as hh24,
  3         extract(minute from intrvl) as mi,
  4         extract(second from intrvl) as ss
  5  from (
  6        select   to_timestamp_tz ('Mon Dec 30 20:21:34 EST 2013','Dy Mon dd hh24:mi:ss TZR yyyy')
  7               - to_timestamp_tz ('Sun Dec 29 11:55:29 EST 2013','Dy Mon dd hh24:mi:ss TZR yyyy') as intrvl
  8       from dual
  9       );

        DD       HH24         MI         SS
---------- ---------- ---------- ----------
         1          8         26          5

答案 1 :(得分:0)

我找到了一个解决方案但它只适用于相同的时区日期。

with tab as (
select to_date(replace(substr('Sun Dec 28 23:59:59 EST 2013', 4), 'EST '), 'Mon DD HH24:MI:SS RRRR') start_date,
       to_date(replace(substr('Tue Dec 30 20:21:34 EST 2013', 4), 'EST '), 'Mon DD HH24:MI:SS RRRR') end_date
  from dual),
tab_sec as
(select ((end_date - start_date) * 24 * 60 * 60) sec from tab)
select lpad(trunc(sec / (60*60)), 2, '0')||':'|| 
       lpad(trunc((sec - (trunc(sec / (60*60)) * 60 * 60))/60), 2, '0')||':'||
       lpad(mod(sec, 60), 2, '0') diff 
 from tab_sec;

答案 2 :(得分:0)

您可以使用NUMTODSINTERVAL

例如

with x as (
   select  to_date('01/01/2014 10:00:00','dd/mm/yyyy hh24:mi:ss') d1 ,
           to_date('01/01/2014 12:00:00','dd/mm/yyyy hh24:mi:ss') d2
   from dual
   union all
   select  to_date('02/01/2014 10:00:00','dd/mm/yyyy hh24:mi:ss') d1 ,
           to_date('01/01/2014 12:00:00','dd/mm/yyyy hh24:mi:ss') d2
   from dual
   union all
   select  to_date('01/01/2014 10:30:00','dd/mm/yyyy hh24:mi:ss') d1 ,
           to_date('01/01/2014 12:00:00','dd/mm/yyyy hh24:mi:ss') d2
   from dual
   union all
   select  to_date('01/01/2014 10:00:30','dd/mm/yyyy hh24:mi:ss') d1 ,
           to_date('01/01/2014 12:00:00','dd/mm/yyyy hh24:mi:ss') d2
   from dual
   union all
   select  to_date('01/01/2014 10:00:30','dd/mm/yyyy hh24:mi:ss') d1 ,
           to_date('02/01/2014 12:20:10','dd/mm/yyyy hh24:mi:ss') d2
   from dual
)
select d1 , d2 , numtodsinterval(d2 - d1, 'day') as interval_diff
from x

D1                  D2                  INTERVAL_DIFF
------------------- ------------------- ---------------------------------
01/01/2014 10:00:00 01/01/2014 12:00:00 +000000000 02:00:00.000000000
02/01/2014 10:00:00 01/01/2014 12:00:00 -000000000 22:00:00.000000000
01/01/2014 10:30:00 01/01/2014 12:00:00 +000000000 01:30:00.000000000
01/01/2014 10:00:30 01/01/2014 12:00:00 +000000000 01:59:30.000000000
01/01/2014 10:00:30 02/01/2014 12:20:10 +000000001 02:19:39.999999999