8

I have to do a "select" that returns the difference between a field of type date from the database and the current date in hours. How can I do this in oracle?

I try this:

   select 24 * (to_date(sysdate, 'YYYY-MM-DD hh24:mi') 
         - to_date('2012-02-28 15:20', 'YYYY-MM-DD hh24:mi')) diff_hours 
   from dual;

But that did not work.

4 Answers 4

24

The error is because SYSDATE is already a date, there's no need to use TO_DATE() to convert it to a date.

If you don't convert it to a date:

select
    24 * (sysdate - to_date('2012-02-28 15:20', 'YYYY-MM-DD hh24:mi')) as diff_hours
from dual;

And if the formatting of the dates are wrong, you can possible use two steps like:

select
    24 * (to_date(to_char(sysdate, 'YYYY-MM-DD hh24:mi'), 'YYYY-MM-DD hh24:mi') - to_date('2012-02-28 15:20', 'YYYY-MM-DD hh24:mi')) as diff_hours
from dual;
1
  • Multiplying by 24 does not give the answer the OP was looking for. In the case of <1 day the answer will be zero. In the case of 1.5 days the answer will be 24. OP specifically asked for number of hours, the answer should be accurate. Commented Sep 27, 2023 at 8:46
3

Diference between two dates in hours and minutes

SELECT     TRUNC(minutes_ / 60) || ' Hours, ' || TRUNC(MOD(minutes_,  60)) || ' Minutes'  diference
FROM     ( SELECT  (sysdate - to_date('16/10/2019 18:45:00', 'DD/MM/YYYY HH24:MI:SS')) * 24 * 60     AS minutes_
  FROM     dual
);

Diference between two dates in hours

SELECT     ROUND(minutes_ / 60, 2) || ' Hours ' 
FROM     ( SELECT  (sysdate - to_date('16/10/2019 18:45:00', 'DD/MM/YYYY HH24:MI:SS')) * 24 * 60     AS minutes_
  FROM     dual
);

Diference between two dates in hours (truncate)

SELECT     ROUND(minutes_ / 60, 2) || ' Hours ' 
FROM     ( SELECT  (sysdate - to_date('16/10/2019 18:45:00', 'DD/MM/YYYY HH24:MI:SS')) * 24 * 60     AS minutes_
  FROM     dual
);
1
  • I used your query to get my desired output in HH:MI:SS SELECT TO_CHAR(TRUNC(Seconds_ / (60*60)),'FM9900') || ':' || TRUNC(MOD(Seconds_, 60*60)/60) || ':' || MOD(Seconds_, 60) elapsed_time FROM ( SELECT (sysdate - (sysdate-31.26/24)) * 24 * 60 * 60 AS Seconds_ FROM dual );
    – Venkat
    Commented Jul 15, 2021 at 11:41
0

I did this way:

select to_number(to_char(sysdate,'HH24')) - to_number(to_char(*YOUR_DATA_VALUE*,'HH24')),max(exp_time)  from ...

Also, you can check this for minutes : Oracle : how to subtract two dates and get minutes of the result

-3
select trunc((sysdate - to_date('2012-02-28 15:20', 'YYYY-MM-DD hh24:mi')) / 24)
  from dual;
2
  • 1
    When subtracting two different dates in Oracle it returns 1 for every day, and 1/24 for an hour Commented Feb 28, 2012 at 19:54
  • You should have multiplied by 24 to get hours from a date difference.
    – Rick Ryker
    Commented Apr 19, 2017 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.