简体   繁体   中英

Oracle SQL Convert number field to hh:mm

I have looked at other similar articles but non are giving me the correct outcome. I have a field called 'Duration' with numbers in minutes stored as an integer. I need to convert that to hh:mm so:

Duration hh:mm
120 02:00
545 09:08
3600 60:00

I've experimented with to_char, to_date, and a few others but not getting anywhere.

You can try using:

  • NUMTODSINTERVAL function, that will transform your integer to seconds
  • TO_CHAR function, that will format your seconds to your required time frame
SELECT Duration, 
       TO_CHAR(TIME'0:0:0'+NUMTODSINTERVAL(Duration,'second'),'hh24:mi:ss')
FROM tab

Check the demo here .

Divide the duration (seconds) by 60. The quotient is the minutes, the remainder is the seconds.

select
  duration,
  to_char(trunc(duration / 60), 'FM00') ||
  ':' ||
  to_char(mod(duration, 60), 'FM00') as time
from mytable;  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM