Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL, checking date relative to "today"

Tags:

sql

postgresql

People also ask

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.

What is now () in PostgreSQL?

The PostgreSQL now function returns the current date and time with the time zone.

How do I get the current date in PostgreSQL?

The PostgreSQL CURRENT_DATE function returns the current date (the system date on the machine running PostgreSQL) as a value in the 'YYYY-MM-DD' format. In this format, 'YYYY' is a 4-digit year, 'MM' is a 2-digit month, and 'DD' is a 2-digit day. The returned value is a date data type.

What is epoch in Postgres?

Posted on 14th August 2022. YES, you can convert EPOCH to Timestamp by merely switching to the present Timestamp in PostgreSQL DBMS. EPOCH time is nothing but the number of seconds from 00:00:00 UTC on 1 January 1970. Till date, without adding the extra leap year days, this is considered.


select * from mytable where mydate > now() - interval '1 year';

If you only care about the date and not the time, substitute current_date for now()

select * from mytable where mydate > current_date - interval '1 year';

I think this will do it:

SELECT * FROM MyTable WHERE mydate > now()::date - 365;

This should give you the current date minus 1 year:

select now() - interval '1 year';

You could also check using the age() function

select * from mytable where age( mydate, now() ) > '1 year';

age() wil return an interval.

For example age( '2015-09-22', now() ) will return -1 years -7 days -10:56:18.274131

See postgresql documentation