How to Get Yesterday’s Date in T-SQL (2023)

Problem:

You would like to display yesterday's date (without time) in an SQL Server database.

Solution:

SELECT DATEADD(day, -1, CAST(GETDATE() AS date)) AS YesterdayDate;

Assuming today is 2020-09-24, the result is:

yesterday_date
2020-09-23

Discussion:

To get yesterday's date, you need to subtract one day from today's date. Use GETDATE() to get today's date (the type is datetime) and cast it to date. In SQL Server, you can subtract or add any number of days using the DATEADD() function.

The DATEADD() function takes three arguments: datepart, number, and date. Here, the value of datepart is day, because the unit of time you want to subtract is day. The second argument is -1 (you subtract 1 day, which is the same as adding -1 day). The third argument is today's date—the date from which you want to subtract.

Of course, you can go back by any interval of time just as easily. Here's an example:

SELECT DATEADD(month, -5, CAST(GETDATE() AS date));

An interval of time can also be added to a date. So, here’s a way if you want to get tomorrow's date:

SELECT DATEADD(day, 1, CAST(GETDATE() AS date)) AS TomorrowDate;
Top Articles
Latest Posts
Article information

Author: Tyson Zemlak

Last Updated: 01/12/2023

Views: 6165

Rating: 4.2 / 5 (63 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Tyson Zemlak

Birthday: 1992-03-17

Address: Apt. 662 96191 Quigley Dam, Kubview, MA 42013

Phone: +441678032891

Job: Community-Services Orchestrator

Hobby: Coffee roasting, Calligraphy, Metalworking, Fashion, Vehicle restoration, Shopping, Photography

Introduction: My name is Tyson Zemlak, I am a excited, light, sparkling, super, open, fair, magnificent person who loves writing and wants to share my knowledge and understanding with you.