How to Get Yesterday's Date in SQL
To get yesterday's date in SQL, use the DATEADD() function. This function takes three arguments: the unit of time you want to add, the number of units you want to add, and the date you want to start with.
To get yesterday's date, you would use the following query:
SELECT DATEADD(day, -1, GETDATE())
This query will return the date from the day before. For example, if today's date is 2020-05-15, the query will return 2020-05-14.
Examples
Let's look at a few examples of how this function can be used. Suppose you want to get the date from two days ago. You would use the following query:
SELECT DATEADD(day, -2, GETDATE())
This query would return the date from two days ago. For example, if today's date is 2020-05-15, the query will return 2020-05-13.
Additional Info
The DATEADD() function is supported by most major databases, including MySQL, PostgreSQL, and SQL Server. However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database. 🤓