PostgreSQL, check date for today

I wonder if anyone can help with some Postgres. I have a table with a mydate column, which is a postgres date type. I want to do something like:

 SELECT * FROM MyTable WHERE mydate > [Today-1year] 

I have never used Postgres before, and I'm sure I just need to know the name of some functions - I will be happy to see a link to myself. Can someone point me in the right direction?

Thank!

+72
sql postgresql
Oct. 13 2018-10-10
source share
4 answers
 select * from mytable where mydate > now() - interval '1 year'; 

If you are only interested in date, not time, replace current_date with now()

+128
Oct 13 '10 at
source share

I think this will do:

 SELECT * FROM MyTable WHERE mydate > now()::date - 365; 
+56
Oct 13 '10 at
source share

This should give you the current date minus 1 year:

 select now() - interval '1 year'; 
+8
Oct 13 '10 at
source share

You can also check with age() function

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

age() wil return the interval.

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

See postgresql documentation

+3
Sep 29 '16 at 8:57
source share



All Articles