SQL GETDATE Minus 1 Day

In today’s tutorial we are going to see how to get minus 1 day from GETDATE() function in SQL and the alternatives of this GETDATE() function let’s start with the introduction first.

Introduction

The GETDATE() function is used to determine the current date in SQL. This function returns the present date in SQL. You can use any online SQL query compiler or you can use the localhost MySQL for executing the queries.

Syntax

The syntax of using is quite simple and it does not require any parameter, you just have to write.

SELECT GETDATE();

The above query will return the current date.

How To Get Minus 1 Day From SQL GETDATE Function

If you want to determine yesterday’s date so try the following query.

SELECT GETDATE() -1

The above query will return the date of yesterday.

Sometimes the GETDATE() date method doesn’t work properly and it shows the error like this “FUNCTION GETDATE does not exist”. 

Here you don’t need to worry about the error because we have alternate methods to find out the current date, we can use the inbuilt date functions of SQL like DATE_ADD and DATE_SUB. Check out the following query.

SQL DATE_ADD Function

SELECT DATE_ADD("2022-08-19", INTERVAL -1 DAY);

The above query will return an output like the below.

Output

2022-08-18

As you can see in the output above it returned the past date. We can determine the next day date means the future date as well you just need to remove that minus sign and add a positive value like below.

SELECT DATE_ADD("2022-08-19", INTERVAL 5 DAY);

In the above query, we are going the check the date after 5 days using the date_add function check out the results below.

Output

2022-08-24

SQL DATE_SUB Function

We can also use the DATE_SUB function, this function also shows the date by subtracting the number of dates from the present one. Check out the following query.

SELECT DATE_SUB("2022-08-19", INTERVAL 1 DAY);

In the above query we used the date_sub function, we passed the current date and interval of 1 day as a parameter let’s see the results.

Output

2022-08-18

As you can see above it returned the date before the current date.

We can also check the date of last week and last month as well like below.

SELECT DATE_SUB("2022-08-19", INTERVAL 2 week);

In the above query, we passed the interval of 2 previous weeks as a parameter let’s check the results.

Output

2022-08-05

Conclusion

In this tutorial we talked about the GETDATE() function of SQL, we saw how to find the date of yesterday with GETDATE() function, then we saw the usage of DATE_ADD() and DATE_SUB()  functions.

We determine the date of past days and future days as well by using the DATE_ADD() and DATE_SUB() function in queries.

That’s all for today hope you like the tutorial if you have queries regarding this article so feel free to contact us below. Thanks for reading.

Suggested Article:

Leave a Comment