1

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

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

我需要了解上述两列之间的差异。我尝试使用“-”运算符,但输出不是预期的方式。
我需要如下输出:00-00-00 00:00:45

4

3 回答 3

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)
于 2013-10-03T05:12:10.937 回答
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;
于 2013-10-03T05:26:12.613 回答
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小提琴

于 2013-10-03T05:42:14.133 回答