日期Oracle中的时间差异计算

时间:2013-10-03 05:00:47

标签: sql oracle

我有一种情况需要获得两列之间的差异。

  

第1栏:01-OCT-13 10:27:15
  第2栏:01-OCT-13 10:28:00

我需要获得上面两列之间的差异。我尝试使用' - '运算符,但输出不是预期的方式 我需要输出如下:00-00-00 00:00:45

3 个答案:

答案 0 :(得分:2)

尝试此查询但未经过测试

SELECT extract(DAY
               FROM diff) days,
       extract(hour
               FROM diff) hours,
       extract(MINUTE
               FROM diff) minutes,
       extract(SECOND
               FROM diff) seconds,
       extract(YEAR
               FROM diff) years
FROM
  (SELECT (CAST (to_date(t.column1, 'yyyy-mm-dd hh:mi:ss') AS TIMESTAMP) -CAST (to_date(t.column2, 'yyyy-mm-dd hh:mi:ss') AS TIMESTAMP)) diff
   FROM tablename AS t)

答案 1 :(得分:2)

试试这个,

WITH T(DATE1, DATE2) AS
(
SELECT TO_DATE('01-OCT-13 10:27:15', 'DD-MON-YY HH24:MI:SS'),TO_DATE('01-OCT-13 10:28:00', 'DD-MON-YY HH24:MI:SS') FROM DUAL
)
SELECT floor(date2 - date1)
          || ' DAYS '
          || MOD(FLOOR ((date2 - date1) * 24), 24)
          || ' HOURS '
          || MOD (FLOOR ((date2 - date1) * 24 * 60), 60)
          || ' MINUTES '
          || MOD (FLOOR ((date2 - date1) * 24 * 60 * 60), 60)
          || ' SECS ' time_difference

FROM T;

答案 2 :(得分:0)

您可以使用以下查询来查找差异 seconds

select 24*60*60*
(to_date('01-OCT-13 10:28:00', 'yyyy-mm-dd hh24:mi:ss')-
to_date('01-OCT-13 10:27:15', 'yyyy-mm-dd hh24:mi:ss')) diff_secs
from dual;

<强> SQL Fiddle