我有一种情况需要获得两列之间的差异。
第1栏:01-OCT-13 10:27:15
第2栏:01-OCT-13 10:28:00
我需要获得上面两列之间的差异。我尝试使用' - '运算符,但输出不是预期的方式 我需要输出如下:00-00-00 00:00:45
答案 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 强>