How to get yesterday's date in MySQL ?

Created at 21-Feb-2024 , By samar

How can i get yesterday's date in MySQL.

In this tutorial you will learn how you can get the yesterday date using inbuilt SQL functions and you can also create own function and call it to get the yesterday's date.

Please execute delimiter // statement in MySQL shell before creating function in MySQL.

Run the below SQL statement to create function -

 create function getYesterdayDate() 
 returns date
 begin
    return adddate(current_date(), interval -1 day);
 end //

Call function using below statement

select getYesterdayDate() //

Output :

mysql> select getYesterdayDate() //
+--------------------+
| getYesterdayDate() |
+--------------------+
| 2024-02-20         |
+--------------------+
1 row in set (0.00 sec)
  • You can use DATE_SUB function to get the yesterday's date in MySQL

    SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS yesterday_date;
    

    The SQL statement SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS yesterday_date; retrieves the date of yesterday from the current date. It uses the CURDATE() function to get the current date and the DATE_SUB() function to subtract one day from it using INTERVAL 1 DAY.

Related Queries

Back to code snippet queries related sql

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.