MySQL DATE_SUB() Function
Definition and Usage
The DATE_SUB() function subtracts a specified time interval from a date.
Syntax
DATE_SUB(date,INTERVAL expr type)
date The parameter is a valid date expression.expr The parameter is the time interval you want to add.
The type parameter can be one of the following values:
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 |
Example
Assuming 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 the "OrderDate".
We use the following SELECT statement:
SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 2 DAY) AS OrderPayDate FROM Orders
Result:
OrderId | OrderPayDate |
---|---|
1 | 2008-12-27 16:25:46.635 |