目前有分钟(bigint
)为
390
我想将此分钟转换为小时。有没有转换它的功能?
Eg: 390 as 6.30
答案 0 :(得分:3)
这样的事情应该有效:
SELECT TO_CHAR('390 minute'::interval, 'HH24:MI')...
回答相关问题:PostgreSQL - How to convert seconds in a numeric field to HH:MM:SS
答案 1 :(得分:1)
您应该能够将它们转换为间隔,然后使用date_part来提取所需的字段
select date_part('hours',interval '1 minute' * minutes) as hours, date_part('minutes',interval '1 minute' * minutes) as minutes;
示例:
dev=# select date_part('hours',interval '1 minute' * 390) as hours, date_part('minutes',interval '1 minute' * 390) as minutes;
hours | minutes
-------+---------
6 | 30
编辑:回复其他评论..
select date_part('hours',interval '1 minute' * minutes) + (date_part('minutes',interval '1 minute' * minutes) * .01);
强制它为数字。
示例:
select date_part('hours',interval '1 minute' * 390) + (date_part('minutes',interval '1 minute' * 390) * .01) as foo;
foo
-----
6.3
(1 row)
转换为数字
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)
或
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)
答案 2 :(得分:1)
你可以拥有PostgreSQL Function
,
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 |
+-------------+