Currently having Minutes(bigint) as
390 I want to convert this Minutes to hours. Is there any function to convert it?
Eg: 390 as 6.30 Something like this should work:
SELECT TO_CHAR('390 minute'::interval, 'HH24:MI')... From an answer to a related question: PostgreSQL - How to convert seconds in a numeric field to HH:MM:SS
You should be able to cast them to an interval and then use date_part to pull out the required fields
select date_part('hours',interval '1 minute' * minutes) as hours, date_part('minutes',interval '1 minute' * minutes) as minutes; example:
dev=# select date_part('hours',interval '1 minute' * 390) as hours, date_part('minutes',interval '1 minute' * 390) as minutes; hours | minutes -------+--------- 6 | 30 edit: in response to other comment..
select date_part('hours',interval '1 minute' * minutes) + (date_part('minutes',interval '1 minute' * minutes) * .01); to force it to a numeric.
example:
select date_part('hours',interval '1 minute' * 390) + (date_part('minutes',interval '1 minute' * 390) * .01) as foo; foo ----- 6.3 (1 row) cast to numeric
select cast(date_part('hours',interval '1 minute' * 390) * 1.0 + (date_part('minutes',interval '1 minute' * 390) * .01) as numeric(4,2)) as foo; foo ------ 6.30 (1 row) or
dev=# select cast(date_part('hours',interval '1 minute' * 240) * 1.0 + (date_part('minutes',interval '1 minute' * 240) * .01) as numeric(4,1)) as foo; foo ----- 4.0 (1 row) you can have a PostgreSQL Function for that,
CREATE OR REPLACE FUNCTION fn_min_to_hrs(mins int) RETURNS numeric AS $BODY$ select cast(date_part('hours',interval '1 minute' * mins) * 1.0 + (date_part('minutes',interval '1 minute' * mins) * .01) as numeric(18,2)); $BODY$ LANGUAGE sql VOLATILE select fn_min_to_hrs(390) Result +-------------+ |fn_min_to_hrs| |numeric(18,2)| +-------------+ | 6.30 | +-------------+
SELECT minutes::numeric / 60