views:

185

answers:

1

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.

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(fieldname, 'HH24:MI:SS') gives an error 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

Can anyone show me where I am going wrong?

+4  A: 
SELECT  TO_CHAR('1000 second'::interval, 'HH24:MI:SS')

or, in your case

SELECT  TO_CHAR((mycolumn || ' second')::interval, 'HH24:MI:SS')
FROM    mytable
Quassnoi
it was the `(...||...)` I was missing. Many thanks!
Shevek
or `mycolumn * '1 second'::interval` which will avoid reparsing the textual format over and over (although it doesn't make much practical difference in this case)
araqnid