SQL SUBDATE()
The SUBDATE() function returns a date or datetime value after subtracting a specified time interval from a starting date. It is commonly used in queries that need to calculate past dates, such as filtering records from a certain number of days, months, or years before a given date.
Syntax
SUBDATE(date, INTERVAL value unit)
Or, alternatively:
SUBDATE(date, days)
Parameters:
date: The startingDATEorDATETIMEvalue.value: The amount of time to subtract.unit: The unit of time to subtract, such asDAY,MONTH,YEAR, orHOUR.days: An integer value representing the number of days to subtract.
Return value:
- Returns a
DATEwhen the input is a date. - Returns a
DATETIMEwhen the input includes a time component.
Example 1: Subtracting a Month from a Date
In this example, SUBDATE() subtracts one month from a given date and returns the resulting date:
SELECT SUBDATE('2026-01-21', INTERVAL 1 MONTH);
The output of this code is:
2025-12-21
Example 2: Shorthand Syntax (Days Only)
In this example, SUBDATE() subtracts a specified number of days from a date using the shorthand numeric syntax:
SELECT SUBDATE('2026-01-21', 10);
The output of this code is:
2026-01-11
Example 3: Using Different Time Units
In this example, SUBDATE() subtracts different time units such as years and hours from date and datetime values, including the current date and time:
SELECT SUBDATE('2026-05-15', INTERVAL 1 YEAR);SELECT SUBDATE('2026-01-21 12:00:00', INTERVAL 3 HOUR);SELECT SUBDATE(NOW(), INTERVAL 1 HOUR);
The output of this code is:
2025-05-152026-01-21 09:00:002026-01-22 11:05:30
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn SQL on Codecademy
- Learn to analyze data with SQL and prepare for technical interviews.
- Includes 9 Courses
- With Certificate
- Beginner Friendly.18 hours
- In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
- Beginner Friendly.5 hours