find the elapsed time between two dates in oracle sql

后端 未结 3 961
囚心锁ツ
囚心锁ツ 2021-02-05 18:23

i need to find the difference between the time in the format hh:mm:ss

select msglog.id,max(msglog.timestamp) enddate,
   min(msglog.timestamp) startdate,
   en         


        
3条回答
  •  無奈伤痛
    2021-02-05 18:56

    Date arithmetic in Oracle results in a number expressed in days. So, to convert to hours, you would multiply by 24 and then trunc to get an integral number:

    trunc(24 * (enddate - startdate))
    

    To get minutes, convert the days value to minutes and mod() that with 60:

    mod(trunc(24 * 60 * (enddate - startdate)), 60)
    

    For seconds, again convert days to seconds and mod() that with 60:

    mod(trunc(24 * 60 * 60 * (enddate - startdate)), 60)
    

    Now you can put these together to get the string value you need.

提交回复
热议问题