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.
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:
And if the formatting of the dates are wrong, you can possible use two steps like:
I did this way:
Also, you can check this for minutes : Oracle : how to subtract two dates and get minutes of the result
Diference between two dates in hours and minutes
Diference between two dates in hours
Diference between two dates in hours (truncate)