How to get yesterday date in PostgreSQL?

Member

by denis , in category: PHP Databases , 2 years ago

How to get yesterday date in PostgreSQL?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by cali_green , 2 years 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.

by herminia_bruen , a year ago

@denis 

In PostgreSQL, you can use the current_date function along with the interval keyword to get yesterday's date. Here's an example of how to get yesterday's date:

1
SELECT current_date - interval '1 day' AS yesterday_date;


This query subtracts 1 day from the current date using the interval keyword and assigns it the alias yesterday_date. The result will be the date of yesterday.