Convert decimal minutes into Minutes:Seconds?

Viewed 322
3 Answers

Using TIMEADD to add number of seconds since midnight:

SELECT TIMEADD(second, 8.5 * 60, '00:00'::TIME) AS res;
                       --arg

Output:

RES
00:08:30

Just build a date from parts, and separate the decimals from the integer minutes. You can create a SQL UDF for convenience:

create function from_decimal_minutes(minutes float)
returns time
as $$
    time_from_parts(0, floor(minutes), 60*(minutes-floor(minutes)))
$$
;

select from_decimal_minutes(8.5);
-- 00:08:30
;

Putting the two excellent solutions already provided together:

SELECT TIME_FROM_PARTS(0,0,8.5*60)

This is nice because it's straight SQL - UDF's may not perform as quick

TIME_FROM_PARTS allows you to pass not just seconds but also minutes,nanoseconds etc.

enter image description here

If you go over 1 day just switch to TIMESTAMP_FROM_PARTS

Below I pass in 5.1 million nanoseconds returning 1 month, 28 days and 40 minutes. This demonstrates the extendibility of the built in functions that can always be used elegantly.

enter image description here

Related