Search code examples
sqloracle-databasedata-conversion

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.


Solution

  • 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;