Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql statement, now - 6 months in unix/epoch time

my database is using an integer in epoch time for date in this table.

I want to do something like this:

select * from myTable where date_column > CURRENT_TIMESTAMP - 6 months

I'm not sure how to get 6 months out of this, dynamically. And the result of CURRENT_TIMESTAMP - 6 months would have to be in epoch time

insight appreciated

like image 919
CQM Avatar asked Sep 04 '25 17:09

CQM


2 Answers

In Postgres, I believe the correct syntax is:

date_column > EXTRACT('epoch' from (NOW() - interval ' 6 months'))

or similarly:

to_timestamp(date_column) > (NOW() - interval ' 6 months'))

You can read the complete documentation of the date/time functions for Postgres for more information

like image 179
JohnnyO Avatar answered Sep 07 '25 17:09

JohnnyO


In MSSQL you can use

select * 
from myTable 
where date_column > dateadd(month,-6,CURRENT_TIMESTAMP)
like image 37
Matt Busche Avatar answered Sep 07 '25 16:09

Matt Busche