5

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 
2
  • possible duplicate: stackoverflow.com/questions/2905692/… Commented Aug 8, 2014 at 12:37
  • 1
    Why not just simply divide it with 60? Like SELECT minutes::numeric / 60 Commented Aug 8, 2014 at 13:37

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

2 Comments

I dont want to convert to char.. Just want to convert my minutes(bigint) to hours(numeric(18,2))
if you want numeric wouldn't 390, be 6.5?
2

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) 

Comments

1

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 | +-------------+ 

       

Comments

0

Not sure if it's even useful for anybody to get 6.30 instead 6.5 but anuway

select concat_ws('.', (390/60)::text, (390%60)::text) 

to get 6.30 as text

select (390/60.0) 

to get 6.5 as float

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.