Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres compare timestamp with long unix timestamp

Tags:

sql

postgresql

I have a timestamp attribute in a table on which I want to place a condition in a sql query where the condition value is a unix timestamp (i.e. numeric long value).

[...] table.timestampattr > 6456454654 [...]

How can I do this?

like image 361
Jack Gibson Avatar asked Sep 11 '13 17:09

Jack Gibson


People also ask

How do I compare two timestamps in PostgreSQL?

To calculate the difference between the timestamps in PostgreSQL, simply subtract the start timestamp from the end timestamp. Here, it would be arrival - departure . The difference will be of the type interval , which means you'll see it in days, hours, minutes, and seconds.

Can you compare dates in PostgreSQL?

PostgreSQL compare date is used to compare date between two different dates, which we have used as an input. We can compare the date by using where and between clauses; we can also compare the date using the date_trunc function in PostgreSQL.

How do I find the timestamp in PostgreSQL?

The PostgreSQL function LOCALTIMESTAMP returns the current date and time (of the machine running that instance of PostgreSQL) as a timestamp value. It uses the 'YYYY-MM-DD hh:mm:ss. nnnnnnn' format, where: YYYY is a 4-digit year.

Does Postgres timestamp have timezone?

Introduction to PostgreSQL timestamp The timestamp datatype allows you to store both date and time. However, it does not have any time zone data. It means that when you change the timezone of your database server, the timestamp value stored in the database will not change automatically.


1 Answers

You can use extract(epoch from ...) to get a Unix timestamp from any of the PostgreSQL time and date types (see Date/Time functions in manual).

So your query fragment could be written:

[...] extract(epoch from table.timestampattr) > 6456454654 [...]

Alternatively, the to_timestamp function performs the opposite conversion, so you could also write:

[...] table.timestampattr > to_timestamp(6456454654) [...]
like image 83
IMSoP Avatar answered Oct 08 '22 16:10

IMSoP