SUBDATE
SUBDATE is a function in SQL that subtracts a specified time period from a date and returns the modified date. It can be used to calculate past dates.
SUBDATE(date, INTERVAL expr unit)
- date: This parameter is the starting date or datetime value. It represents where the operation begins. Date or datetime expressions can be used or literal string providing a valid date or datetime format.
- interval expr unit: This is a combination of two sub-parameters, expr and unit. Expr represents the interval you want to subtract and unit is the type of interval being subtracted e.g., day, hour, month, year, etc. The interval value is an integer, and the unit can be in days, hours, minutes, seconds, months, or years.
Example
Output
Explanation
The SUBDATE
function subtracts a specified time interval from a date. In the provided example, 3 days are subtracted from ‘2022-01-01’, resulting in the output ‘2021-12-29’.