How to get yesterday date in PostgreSQL?

Member

by denis , in category: PHP Databases , 8 months ago

How to get yesterday date in PostgreSQL?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 3 months ago

@denis 

To get yesterday's date in PostgreSQL, you can use the current_date function and the interval function.


Here is an example of how to use these functions to get yesterday's date:

1
SELECT current_date - interval '1 day' as yesterday;


This will return a date value representing yesterday's date.


You can also use the date_trunc function to truncate the time portion of the current date, and then subtract one day to get yesterday's date:

1
SELECT date_trunc('day', current_date) - interval '1 day' as yesterday;


Note that these examples assume that your PostgreSQL server's timezone is set to the local timezone. If the server's timezone is set to a different timezone, the results of these queries may not be what you expect.