1

I have a situation where I need to get the differences between two columns.

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

I need to get the differences between those above two columns. I tried using '-' operator but the output is not in an expected way.
I need output as follows: 00-00-00 00:00:45

Noel
  • 10,152
  • 30
  • 45
  • 67
user2037445
  • 161
  • 1
  • 2
  • 11
  • 2
    How can a time be `10:27:60`? That would be `10:28:00` (60 seconds in a minute). Are these `DATE` columns (in which case the trailing 60 is not valid)? Or are they `timestamp` columns (in which case the trailing 60 may be intended to represent fractional seconds and you're just missing the whole number of seconds)? – Justin Cave Oct 03 '13 at 05:02
  • Oh sorry...my mistake.. :) – user2037445 Oct 03 '13 at 05:03
  • Columns are of type DATE – user2037445 Oct 03 '13 at 05:05

3 Answers3

2

Try this query but it is not tested

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)
Swapnil Patil
  • 971
  • 2
  • 18
  • 41
2

Try this too,

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;
Dba
  • 6,511
  • 1
  • 24
  • 33
0

You can use the below query to find the difference 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

Pooh
  • 107
  • 1
  • 5