Definition and usage
The Date_sub () function subtracts the specified time interval from the date.
Grammar
Date_sub (Date,interval expr type)
The date parameter is a valid day expression. The expr parameter is the time interval that you want to add.
The type parameter can be the following value:
| Type value |
| Microsecond |
| SECOND |
| MINUTE |
| HOUR |
| Day |
| WEEK |
| MONTH |
| QUARTER |
| Year |
| Second_microsecond |
| Minute_microsecond |
| Minute_second |
| Hour_microsecond |
| Hour_second |
| Hour_minute |
| Day_microsecond |
| Day_second |
| Day_minute |
| Day_hour |
| Year_month |
Instance
Suppose we have the following table:
| OrderId |
ProductName |
OrderDate |
| 1 |
' Computer ' |
2008-12-29 16:25:46.635 |
Now, we want to subtract 2 days from "OrderDate".
We use the following SELECT statement:
SELECT OrderId, as DATE_SUB(OrderDate,INTERVAL 2 DAY) orderpaydatefrom Orders
Results:
| OrderId |
orderpaydate |
| 1 |
2008-12-27 16:25:46.635 |
MySQL Date function
MySQL date_sub () function