Oracle Convert Seconds to Hours:Minutes:Seconds

前端 未结 14 1118
再見小時候
再見小時候 2020-12-08 06:01

I have a requirement to display user available time in Hours:Minutes:Seconds format from a given total number of seconds value. Appreciate if you know a ORACLE function to d

14条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 06:20

    For the comment on the answer by vogash, I understand that you want something like a time counter, thats because you can have more than 24 hours. For this you can do the following:

    select to_char(trunc(xxx/3600)) || to_char(to_date(mod(xxx, 86400),'sssss'),':mi:ss') as time
    from dual;
    

    xxx are your number of seconds.

    The first part accumulate the hours and the second part calculates the remaining minutes and seconds. For example, having 150023 seconds it will give you 41:40:23.

    But if you always want have hh24:mi:ss even if you have more than 86000 seconds (1 day) you can do:

    select to_char(to_date(mod(xxx, 86400),'sssss'),'hh24:mi:ss') as time 
    from dual;
    

    xxx are your number of seconds.

    For example, having 86402 seconds it will reset the time to 00:00:02.

提交回复
热议问题