DATE_SUB
DATE_SUB is a SQL function that subtracts a certain number of days, weeks, months, or years from a given date, resulting in a new date value. The type of interval to subtract (day, week, etc.) and the quantity to subtract are specified as parameters within the function.
DATE_SUB(date, INTERVAL expr type)
- date: This parameter specifies the date from which the interval needs to be subtracted. It must be a valid date value that MySQL supports, including date expressions that return a date or datetime value.
- interval expr type: The INTERVAL keyword and its following expression (expr) mean to subtract a certain time interval from the date we have in the first parameter. It has two parts
Example
Output
Explanation
The above example demonstrates the usage of DATE_SUB function in MySQL. It subtracts a time interval from the provided date. In this case the interval is 31 days, subtracted from ‘2008-01-02’, which results in ‘2007-12-02’.