HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Converting a Unix timestamp to an SQL timestamp in Postgres

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
unixpostgressqltimestampconverting

Problem

I have passed the result of Date.now() and other plain Unix timestamp values like 1534360109480 into SQL and ran them through this:

"@converted_date" = to_timestamp("@date") AT TIME ZONE 'UTC';

And it consistently returns 50591-11-28 22:32:38.

I do not understand how the year could possibly be 50591 and the date/time is not accurate beyond that anyways.

What am I doing wrong?

Solution

to_timestamp accepts the epoch value ("Unix time") with second precision, while your value seems to have millisecond precision. You need to do something like

to_timestamp(@date/1000) AT TIME ZONE 'UTC';

Code Snippets

to_timestamp(@date/1000) AT TIME ZONE 'UTC';

Context

StackExchange Database Administrators Q#215018, answer score: 6

Revisions (0)

No revisions yet.