简体   繁体   中英

PostgreSQL, checking date relative to “today”

Was wondering if someone could assist with some Postgres. I have a table which has a column called mydate which is a postgres date type. I want to do something like:

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

I've never used Postgres before and I'm sure I just need to know the name of some functions- I'll gladly look up the reference myself. Can anyone point me in the right direction?

Thanks!

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

如果您只关心日期而不关心时间,请将current_date替换为now()

我想这会做到:

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

这应该给你当前日期减去1年:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM