How to Subtract 30 Days from a Date in SQL

With the DATE_SUB() function, you can quickly and easily subtract 30 days from any date in SQL.

This function takes two arguments: the date you want to subtract from, and the number of days you want to subtract. For example, if you wanted to subtract 30 days from the date 2020-01-01, you would use the following query:

SELECT DATE_SUB('2020-01-01', INTERVAL 30 DAY);

Examples

Let's look at a few examples of how this function can be used. Suppose you have a table called orders with the following data:

orders
order_date
2020-01-01
2020-02-01
2020-03-01
2020-04-01


If you wanted to subtract 30 days from each of these dates, you would use the following query:

SELECT DATE_SUB(order_date, INTERVAL 30 DAY) FROM orders;

This query would return the following results:


2020-12-02
2020-01-02
2019-12-02
2019-11-02

Additional Info

The DATE_SUB() 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.

Want to build your own LLM Apps with AirOps👇👇