How to get today and yesterday in PosgreSQL

Very useful dates functions for PostgreSQL.

The official documentation is here.

Getting the current date and time

SELECT NOW()

Getting the current day, no time

SELECT date_trunc('day', NOW())::date;

One hour later

SELECT (NOW() + interval '1 hour') AS one_hour_later;

One day later

SELECT NOW()+1

The day before

SELECT NOW()-1

For more complex calculations you can use intervals

For example:

SELECT (NOW() + interval '1 day') AS now_but_tomorrow;

Other articles for databases geeks

comments powered by Disqus