MySQL DATE_SUB

Syntax :

DATE_SUB(date, INTERVAL expr unit)

DATE_SUB() function returns a date after which a certain time/date interval has been subtracted.

Parameters of MySQL DATE_SUB() function : 

date : The date is to which the interval should be subtracted.

value : The value is the date or time interval to subtract. This value can be both positive and negative.

unit : The unit is the type of interval to subtract such as SECOND, MINUTE, HOUR, DAY, YEAR, MONTH etc.

 

unit Value Expected expr Format
MICROSECOND MICROSECONDS
SECOND SECONDS
MINUTE MINUTES
HOUR HOURS
DAY DAYS
WEEK WEEKS
MONTH MONTHS
QUARTER QUARTERS
YEAR YEARS
SECOND_MICROSECOND 'SECONDS.MICROSECONDS'
MINUTE_MICROSECOND 'MINUTES.MICROSECONDS'
MINUTE_SECOND 'MINUTES:SECONDS'
HOUR_MICROSECOND 'HOURS.MICROSECONDS'
HOUR_SECOND 'HOURS:MINUTES:SECONDS'
HOUR_MINUTE 'HOURS:MINUTES'
DAY_MICROSECOND 'DAYS.MICROSECONDS'
DAY_SECOND 'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE 'DAYS HOURS:MINUTES'
DAY_HOUR 'DAYS HOURS'
YEAR_MONTH 'YEARS-MONTHS'

 

Examples of DATE_SUB() function

Example 1 : Subtract 20 days to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL 20 DAY) as date; 

// output : 2022-04-25 09:25:18 

 

Example 2 : Subtract 11 months to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL 11 MONTH) as date; 

// output : 2021-06-15 09:25:18 

 

Example 3 : Add 2 months to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL -2 MONTH) as date; 

// output : 2022-07-15 09:25:18 

 

Example 4 : Add 1 year to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL 1 YEAR) as date; 

// output : 2021-05-15 09:25:18 

 

Example 5 : Subtract 12 weeks to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL 12 WEEK) as date; 

// output : 2022-02-20 09:25:18 

 

Example 6 : Subtract 12 hours to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL 12 HOUR) as date; 

// output : 2022-05-14 21:25:18 

 

Example 7 : Add 2 hours to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL -2 HOUR) as date; 

// output : 2022-05-15 11:25:18 

 

Example 8 : Subtract 12 minutes to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL 12 MINUTE) as date; 

// output : 2022-05-15 09:13:18 

 

Example 9 : Add 5 minutes to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL -5 MINUTE) as date; 

// output : 2022-05-15 09:30:18 

 

Example 10 : Subtract 12 seconds to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL 12 SECOND) as date; 

// output : 2022-05-15 09:25:06 

 

Example 11 : Subtract 7 days & seconds to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL '7 1:03:12' DAY_SECOND) as date; 

// output : 2022-05-08 08:22:06 

 

Example 12 : Subtract 7 days & minutes to a date and return the date: 

mysql> SELECT DATE_SUB("2022-05-15 09:25:18", INTERVAL '7 1:03' DAY_MINUTE) as date; 

// output : 2022-05-08 08:22:18