Skip to content

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

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

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

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

Constraints

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