PostgreSQL - 如何将数字字段中的秒数转换为 HH:MM:SS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2905692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostgreSQL - How to convert seconds in a numeric field to HH:MM:SS
提问by Shevek
I'm new to PostgreSQL (I have been using MS SQL for many years) and need to convert a numeric column which contains a time in seconds to HH:MM:SS format.
我是 PostgreSQL 的新手(我已经使用 MS SQL 很多年了)并且需要将包含以秒为单位的时间的数字列转换为 HH:MM:SS 格式。
I have Googled and found that to_char(interval '1000s', 'HH24:MI:SS')
works so I am attempting to use this with my field name:
我已经谷歌搜索并发现to_char(interval '1000s', 'HH24:MI:SS')
有效,所以我试图将其与我的字段名称一起使用:
to_char(fieldname, 'HH24:MI:SS')
gives an error cannot use "S" and "PL"/"MI"/"SG"/"PR" together
to_char(fieldname, 'HH24:MI:SS')
给出错误 cannot use "S" and "PL"/"MI"/"SG"/"PR" together
to_char(fieldname::interval, 'HH24:MI:SS')
gives an error cannot cast type numeric to interval
to_char(fieldname::interval, 'HH24:MI:SS')
给出错误 cannot cast type numeric to interval
Can anyone show me where I am going wrong?
谁能告诉我我哪里出错了?
回答by Quassnoi
SELECT TO_CHAR('1000 second'::interval, 'HH24:MI:SS')
or, in your case
或者,就你而言
SELECT TO_CHAR((mycolumn || ' second')::interval, 'HH24:MI:SS')
FROM mytable
回答by Nilesh Yadav
To convert seconds to HH:MM:SS
将秒转换为 HH:MM:SS
SELECT 120 * interval '1 sec';
You will get 00:02:00
您将获得 00:02:00
To convert minutes to HH:MM:SS
将分钟转换为 HH:MM:SS
SELECT 120 * interval '1 min';
You will get 02:00:00
您将获得 02:00:00