how to get yesterday date in sql

How to Get Yesterday's Date in SQL

By Sarah Taylor
AD

Ever found yourself needing to retrieve data from the previous day in SQL? Whether you're tracking sales, analyzing user activity, or monitoring system logs, knowing how to obtain yesterday's date can be incredibly useful. Here's a step-by-step guide on how to achieve this, optimized for search engines and written in a clear, human-friendly style.

First, let's understand that SQL doesn't have a built-in function to get yesterday's date. However, we can use common SQL functions and syntax to achieve this. Let's dive right in.

Using INTERVAL in PostgreSQL

PostgreSQL provides an easy way to get yesterday's date using the INTERVAL function. INTERVAL allows you to subtract or add a specific period of time to a date.

AD
SQL GETDATE() Explained with Examples | Current Date & Time in SQL Server
SQL GETDATE() Explained with Examples | Current Date & Time in SQL Server
Learn SQL just by investing 30 minutes a day within 30 Days
Learn SQL just by investing 30 minutes a day within 30 Days
a table with numbers and dates for different events in the year 2012 - 2013, including two
a table with numbers and dates for different events in the year 2012 - 2013, including two

Here's a simple SQL query to get yesterday's date using INTERVAL:

```sql SELECT CURRENT_DATE - INTERVAL '1 day'; ```

Explanation

The CURRENT_DATE function returns the current date, and INTERVAL '1 day' subtracts one day from the current date, effectively giving us yesterday's date.

Sql basics for data analytics
Sql basics for data analytics
a whiteboard with the words day 2 retrieving data with selectr on it
a whiteboard with the words day 2 retrieving data with selectr on it
WHERE vs HAVING in SQL Explained Simply | Easy SQL Cheat Sheet for Beginners
WHERE vs HAVING in SQL Explained Simply | Easy SQL Cheat Sheet for Beginners

Here's an example. If today's date is '2022-04-24', the query will return '2022-04-23'.

Getting yesterday's date with time

If you need yesterday's date with time, you can use the following query:

AD
```sql SELECT CURRENT_TIMESTAMP - INTERVAL '1 day'; ```

CURRENT_TIMESTAMP includes the time, so you'll get yesterday's date at the same time as the current timestamp. For instance, if the current timestamp is '2022-04-24 12:34:56', the query will return '2022-04-23 12:34:56'.

an image of a computer screen with the text's name and numbers on it
an image of a computer screen with the text's name and numbers on it
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL ORDER BY Explained with Examples | Sort Data in SQL
a whiteboard with some writing on it
a whiteboard with some writing on it
30 SQL COMMANDS EVERY DATABASE DEVELOPER SHOULD MASTER
30 SQL COMMANDS EVERY DATABASE DEVELOPER SHOULD MASTER
Ultimate SQL Roadmap 💻 | Learn SQL in 30 Days (Step-by-Step)
Ultimate SQL Roadmap 💻 | Learn SQL in 30 Days (Step-by-Step)
an info sheet with diagrams and text on it
an info sheet with diagrams and text on it

Using DATEADD in SQL Server

SQL Server uses the DATEADD function to add or subtract dates. Here's how you can get yesterday's date in SQL Server:

```sql SELECT DATEADD(day, -1, GETDATE()); ```

Explanation

DATEADD subtracts one day (indicated by -1) from the current date and time returned by the GETDATE() function.

AD

For example, if the current date and time are '2022-04-24 12:00:00', SQL Server will return '2022-04-23 12:00:00'.

How to Find Monthly Revenue Using SQL | SQL for Data Analysts
How to Find Monthly Revenue Using SQL | SQL for Data Analysts
a poster with the words and instructions for an application to learn how to use it
a poster with the words and instructions for an application to learn how to use it
SQL JOINS Explained: A Simple Guide for Data Science Beginners
SQL JOINS Explained: A Simple Guide for Data Science Beginners

Getting yesterday's date with time

To get yesterday's date with time, you can use the same query. DATEADD in SQL Server includes the time by default, so if you run the query now, it will return yesterday's date at the same time as the current timestamp.

Using DATE_SUB in MySQL

MySQL uses the DATE_SUB function to subtract dates. To get yesterday's date in MySQL, you can use the following query:

AD
```sql SELECT CURDATE() - INTERVAL 1 DAY; ```

Explanation

CURDATE() returns the current date, and INTERVAL 1 DAY subtracts one day from the current date.

For example, if today's date is '2022-04-24', MySQL will return '2022-04-23'.

Getting yesterday's date with time

To get yesterday's date with time in MySQL, you can use the NOW() function instead of CURDATE():

```sql SELECT NOW() - INTERVAL 1 DAY; ```

NOW() returns the current date and time. Subtracting one day will give you yesterday's date with the same time as the current timestamp.

Remember, SQL is case-insensitive, but its functions are not. Make sure you use the correct function name as shown above.

Now that you know how to get yesterday's date in SQL, you can start writing more efficient queries. Keep practicing and exploring different SQL dialect-specific syntax to become a pro in no time. Happy coding!

AD