MySQL SUBDATE() Function

In MySQL, the SUBDATE() function subtracts a date or time interval from the specified date or datetime and returns the result.

It is similar to DATE_SUB().

SUBDATE() Syntax

Here is the syntax of MySQL SUBDATE() function:

SUBDATE(date, days)
SUBDATE(date, INTERVAL value unit)

Parameters

date
Required. Date the action is required.
days
Required. The number of days to subtract from date .
value
Required. The date/time interval. Both positive and negative numbers are allowed.
unit
Required. The unit of the date/time interval.

The unit of the date/time interval can be one of the following values:

  • 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

Return value

The SUBDATE() function subtracts a date or time interval from the specified date or datetime and returns the result. The result’s date type is determined by the parameters:

  • If date is a DATE value and unit is YEAR, MONTH or DAY, it returns a DATE value.
  • If date is a DATE value and unit is HOURS, MINUTES or SECONDS, it returns a DATETIME value.
  • If date is a DATETIME value, it returns a DATETIME value.
  • If date is a TIME value and unit is YEAR, MONTH or DAY, it returns a DATETIME value.
  • If date is a TIME value and the computation involves only the HOURS, MINUTES and SECONDS parts , it returns a TIME value. (MySQL 8.0.28 and later)
  • Otherwise it returns a string value.

SUBDATE() Examples

Here are some examples of the SUBDATE() function.

Example 1

SELECT
    SUBDATE('2020-06-10', 10),
    SUBDATE('2020-06-10', -10)\G
 SUBDATE('2020-06-10', 10): 2020-05-31
SUBDATE('2020-06-10', -10): 2020-06-20

Example 2

SELECT
    SUBDATE('2020-06-10', INTERVAL 10 DAY),
    SUBDATE('2020-06-10', INTERVAL 10 HOUR)\G
 SUBDATE('2020-06-10', INTERVAL 10 DAY): 2020-05-31
SUBDATE('2020-06-10', INTERVAL 10 HOUR): 2020-06-09 14:00:00

Example 3

SELECT
    SUBDATE('2020-06-10 10:00:00', INTERVAL 10 HOUR),
    SUBDATE('2020-06-10 10:00:00', INTERVAL 10 MINUTE)\G
  SUBDATE('2020-06-10 10:00:00', INTERVAL 10 HOUR): 2020-06-10 00:00:00
SUBDATE('2020-06-10 10:00:00', INTERVAL 10 MINUTE): 2020-06-10 09:50:00

Example 4

SELECT
    SUBDATE(CURDATE(), INTERVAL 10 HOUR),
    SUBDATE(NOW(), INTERVAL 10 MINUTE)\G
SUBDATE(CURDATE(), INTERVAL 10 HOUR): 2022-04-10 14:00:00
  SUBDATE(NOW(), INTERVAL 10 MINUTE): 2022-04-11 08:56:43

Here, we used the CURDATE() function and the NOW() function to return the current date and datetime.