Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MySQL DATE_SUB() function


May 15, 2021 MySQL


Table of contents


MySQL DATE_SUB() function


MySQL DATE_SUB() function MySQL Date function

Definitions and usages

DATE_SUB () function subtracts the specified interval from the date.

Grammar

DATE_SUB(date,INTERVAL expr type)

The date argument is a legitimate date expression. The expr parameter is the interval 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

Let's say we have the following "Orders" table:

OrderId ProductName OrderDate
1 Jarlsberg Cheese 2008-11-11 13:23:44.657

Now, we want to subtract 5 days from OrderDate.

Let's use the following SELECT statement:

SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 5 DAY) AS SubtractDate
FROM Orders

Results:

OrderId SubtractDate
1 2008-11-06 13:23:44.657


MySQL DATE_SUB() function MySQL Date function