This tutorial explains how to use the MySQL DATE_SUB function with syntax and examples.
The MySQL DATE_SUB function returns a date after which a certain time/date interval has been subtracted.
The syntax for the DATE_SUB function in MySQL is:
The date to which the interval should be subtracted.
The value of the time/date interval that you wish to subtract. You can specify positive and negative values for this parameter.
The unit type of the interval such as DAY, MONTH, MINUTE, HOUR, and so on. It can be one of the following:
unit | Compatibility |
---|---|
MICROSECOND | 4.1.1+ |
SECOND | 3.2.3+ |
MINUTE | 3.2.3+ |
HOUR | 3.2.3+ |
DAY | 3.2.3+ |
WEEK | 5+ |
MONTH | 3.2.3+ |
QUARTER | 5+ |
YEAR | 3.2.3+ |
SECOND_MICROSECOND | 4.1.1+ |
MINUTE_MICROSECOND | 4.1.1+ |
MINUTE_SECOND | 4.1.1+ |
HOUR_MICROSECOND | 4.1.1+ |
HOUR_SECOND | 4.1.1+ |
HOUR_MINUTE | 3.2.3+ |
DAY_MICROSECOND | 4.1.1+ |
DAY_SECOND | 3.2.3+ |
DAY_MINUTE | 3.2.3+ |
DAY_HOUR | 3.2.3+ |
YEAR_MONTH | 3.2.3+ |
The DATE_SUB function can be used in the following versions of MySQL:
Let's look at some MySQL DATE_SUB function examples and explore how to use the DATE_SUB function in MySQL.
For example: