YouTip LogoYouTip

Func Date Sub

MySQL DATE_SUB() Function

MySQL DATE_SUB() Function

-- Learning is not just about technology, but also about dreams!

SQL Tutorial

SQL Advanced Tutorial

SQL Functions


MySQL DATE_SUB() Function

The DATE_SUB() function subtracts a specified time interval from a date.

Syntax

DATE_SUB(date,INTERVAL expr type)

Parameters

ParameterDescription
dateThe date.
exprThe expression.
typeThe time interval type.

Possible Time Interval Types

TypeDescription
MICROSECONDMicroseconds
SECONDSeconds
MINUTEMinutes
HOURHours
DAYDays
WEEKWeeks
MONTHMonths
QUARTERQuarters
YEARYears
SECOND_MICROSECONDSeconds and microseconds
MINUTE_MICROSECONDMinutes and microseconds
MINUTE_SECONDMinutes and seconds
HOUR_MICROSECONDHours and microseconds
HOUR_SECONDHours and seconds
HOUR_MINUTEHours and minutes
DAY_MICROSECONDDays and microseconds
DAY_SECONDDays and seconds
DAY_MINUTEDays and minutes
DAY_HOURDays and hours
YEAR_MONTHYears and months

Example

We have the following table below:

OrderId  ProductName  OrderDate
1        Computer     2008-12-29

Now, we want to subtract 5 days from OrderDate.

SQL:

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

The result will be:

OrderId  SubtractDate
1        2008-12-24

Example with Multiple Intervals

Subtract 1 year, 2 months, and 3 days from the date:

SELECT DATE_SUB('2008-12-29', INTERVAL '1-2-3' YEAR_MONTH_DAY) AS NewDate;

Note: MySQL does not support the YEAR_MONTH_DAY combined type directly; use separate operations or the DATE_ADD() function with negative intervals. For completeness, the example above is illustrative. Use INTERVAL 1 YEAR + INTERVAL 2 MONTH + INTERVAL 3 DAY instead.

← Func Datediff MysqlFunc Date Add β†’