DATE_SUB()

Description

The DATE_SUB() function subtracts a time/date interval from a date and then returns the date. If date is NULL, the function returns NULL.

Syntax

1. DATE_SUB(date,INTERVAL expr unit)

Arguments

Arguments Description
date Required. The date/datetime to extract the date from.
expr Required. The expr is an expression specifying the interval value to be added or subtracted from the starting date. The expr is evaluated as a string; it may start with a - for negative intervals.
unit Required. The unit is a keyword indicating the units in which the expression should be interpreted. The unit argument can have the following values: MICROSECOND SECOND MINUTE HOUR DAY WEEK MONTH QUA TER YEAR SECOND_MICROSECOND MINUTE_MICROSECOND MINUTE_SECOND HOUR_MICROSECOND HOUR_SECOND HOUR_MINUTE DAY_MICROSECOND DAY_SECOND DAY_MINUTE DAY_HOUR YEAR_MONTH

Examples

1. create table t2(orderid int, productname varchar(20), orderdate datetime);
2. insert into t2 values ('1','Jarl','2008-11-11 13:23:44.657');

4. mysql> SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 5 DAY) AS SubtractDate FROM t2;
5. +---------+---------------------+
6. | orderid | subtractdate        |
7. +---------+---------------------+
8. |       1 | 2008-11-06 13:23:45 |
9. +---------+---------------------+
10. 1 row in set (0.01 sec)

Constraints

The date type supports only yyyy-mm-dd and yyyymmdd for now.