SQL Get all records older than 30 days

SqlPostgresql

Sql Problem Overview


Now I've found a lot of similar SO questions including an old one of mine, but what I'm trying to do is get any record older than 30 days but my table field is unix_timestamp. All other examples seem to use DateTime fields or something. Tried some and couldn't get them to work.

This definitely doesn't work below. Also I don't want a date between a between date, I want all records after 30 days from a unix timestamp stored in the database. I'm trying to prune inactive users.

simple examples.. doesn't work.

SELECT * from profiles WHERE last_login < UNIX_TIMESTAMP(NOW(), INTERVAL 30 DAY)  

And tried this

SELECT * from profiles WHERE UNIX_TIMESTAMP(last_login - INTERVAL 30 DAY) 

Not too strong at complex date queries. Any help is appreciate.

Sql Solutions


Solution 1 - Sql

Try something like:

SELECT * from profiles WHERE to_timestamp(last_login) < NOW() - INTERVAL '30 days' 

Quote from the manual:

> A single-argument to_timestamp function is also available; it accepts a double precision argument and converts from Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp with time zone. (Integer Unix epochs are implicitly cast to double precision.)

Solution 2 - Sql

Unless I've missed something, this should be pretty easy:

SELECT * FROM profiles WHERE last_login < NOW() - INTERVAL '30 days';

Solution 3 - Sql

How about

SELECT * from profiles WHERE last_login < VALUEOFUNIXTIME30DAYSAGO

or

SELECT * from profiles WHERE last_login < (extract(epoch from now())-2592000)

Have a look at this post:

https://dba.stackexchange.com/questions/2796/how-do-i-get-the-current-unix-timestamp-from-postgresql

and this

http://www.epochconverter.com/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPanama JackView Question on Stackoverflow
Solution 1 - SqlIhor RomanchenkoView Answer on Stackoverflow
Solution 2 - SqlJan SpurnyView Answer on Stackoverflow
Solution 3 - SqltanstaaflView Answer on Stackoverflow