Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert seconds to yyyy-mm-dd hh:mm:ss PostgreSQL

I am new to PostgreSQL sql and now working on an already existing database. I have a column called value in a table and it contains datetime stamp in seconds. So I am looking for way I can convert those seconds to yyyy-mm-dd hh:mm:ss in a Postgres database. I tried the following:

SELECT  TO_CHAR('1444646136079 second'::interval, 'yyyy-mm-dd HH24:MI:SS')

1444646136079 is the value I would like to convert But I am getting the following error:

ERROR: interval field value out of range: "1444646136079 second"

like image 214
kya Avatar asked Oct 22 '15 08:10

kya


1 Answers

An interval is not a "timestamp".

The to_timestamp() function accepts seconds as an input and can convert that to a proper timestamp. I believe your 1444646136079 is in fact milli seconds, not seconds:

select to_char(to_timestamp(1444646136079 / 1000), 'yyyy-mm-dd HH24:MI:SS')

returns:

2015-10-12 12:35:36
like image 140
a_horse_with_no_name Avatar answered Oct 30 '22 14:10

a_horse_with_no_name