Skip to content

DATE_SUB()

Function Description

DATE_SUB() is used to perform date operations: DATE_SUB() function subtracts the specified time interval from the date date. If date is NULL, the function returns NULL.

Function Syntax

DATE_SUB(date,INTERVAL expr unit)

Parameter definition

Parameters Description
date Required parameters. The date parameter is a legal date expression.
expr Required parameters. The expr parameter is the time interval that needs to be added to date. If expr is a negative number, it can start with "-".
unit Required parameters. The unit parameter can be the following values:
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUA
TER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_SECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_MICROSECOND
DAY_MICROSECOND
DAY_MICROSECOND
DAY_MICROSECOND
YEAR_MONTH

Example

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)

limit

Currently, the date format only supports the data formats of yyyy-mm-dd and yyyymmdd.