How to subtract 2 dates in oracle to get the result in hour and minute

后端 未结 8 1888
深忆病人
深忆病人 2020-12-16 20:49

I want to subtract 2 dates and represent the result in hour and minute in one decimal figure.

I have the following table and I am doing it in this way but the result

8条回答
  •  既然无缘
    2020-12-16 21:06

    Oracle represents dates as a number of days, so (end_time-start_time)*24 gives you hours. Let's assume you have this number (eg. 2.9166667) in h column. Then you can easily convert it to the format you want with: FLOOR(h) + (h-FLOOR(h))/100*60.

    Example:

    WITH diff AS (
        SELECT (TO_DATE('21-06-2011 16:55:00', 'DD-MM-YYYY HH24:MI:SS') - TO_DATE('21-06-2011 14:00:00', 'DD-MM-YYYY HH24:MI:SS'))*24 h
        FROM dual
    ) SELECT FLOOR(h) + (h-FLOOR(h))/100*60
    FROM diff
    

    In your case:

    SELECT start_time, end_time,
        FLOOR((end_time-start_time)*24) + ((end_time-start_time)*24-FLOOR((end_time-start_time)*24))/100*60 AS hours_diff
    FROM come_leav 
    

提交回复
热议问题