I have two dates : Date1: 20-Mar-2020 07:35:00 PM
and Date2: 20-Mar-2020 02:42:00 PM
I have written the query :
SELECT ROUND(minutes / 60, 2) || ' Hours ' FROM ( SELECT (Date1 - to_date(Date2, 'DD-MON-YYYY HH12:MI:SS')) * 24 * 60 AS minutes FROM dual );
But I am not getting the desired result it is displaying 19.58 hours which is wrong , please correct me where am I going Wrong ?
Advertisement
Answer
As commented by Mat, you need to convert both strings to date, then substract them:
to_date(date1, 'dd-mon-yyyy hh12:mi:ss am') - to_date(date2, 'dd-mon-yyyy hh12:mi:ss am')
Note that your format specifier was missing the am
part.
This gives you a result as a decimal number of days, that you can then convert th hours by multiplying it by 24:
round( ( to_date(date1, 'dd-mon-yyyy hh12:mi:ss am') - to_date(date2, 'dd-mon-yyyy hh12:mi:ss am') ) * 24, 2 ) || ' hours'
select round( ( to_date('20-Mar-2020 07:35:00 PM', 'dd-mon-yyyy hh12:mi:ss am') - to_date('20-Mar-2020 02:42:00 PM', 'dd-mon-yyyy hh12:mi:ss am') ) * 24, 2 ) || ' hours' res from dual
| RES | | :--------- | | 4.88 hours |