w3resource

MySQL SUBDATE() function

SUBDATE() function

MySQL SUBDATE() subtracts a time value (as interval) from a given date.

DATE_SUB() and SUBDATE are synonyms of SUBDATE().

This function is useful in -

  • Date Manipulation: SUBDATE() is useful for manipulating dates relative to a given date, making it easy to find previous or future dates.
  • The function is valuable for projecting dates into the past or future, which is essential for planning and forecasting.
  • SUBDATE() supports calculating dates in different formats, such as days, months, or years, based on your specific needs.
  • SUBDATE() helps validate and adjust date values, ensuring they remain within meaningful date ranges.
  • It allows you to transform date values based on a defined interval, making it suitable for various data transformation tasks.
  • It is valuable for time series analysis by allowing you to generate a series of dates based on a reference date.
  • It is valuable for time series analysis by allowing you to generate a series of dates based on a reference date.

Syntax:

SUBDATE(date, INTERVAL expr unit)

Arguments:

Name Description
date A date value.
INTERVAL Keyword.
expr A date or datetime expression or a number.
unit An unit, described in the following table.

MySQL Version: 8.0

Pictorial Presentation:

Pictorial Presentation of MySQL SUBDATE() function

Example: MySQL SUBDATE() function

The following statement will return a date after subtracting 10 days (notice that INTERVAL keyword is used) from the specified date 2008-05-15.

Code:

SELECT SUBDATE('2008-05-15', INTERVAL 10 DAY);

Output:

mysql> SELECT SUBDATE('2008-05-15', INTERVAL 10 DAY);
+----------------------------------------+
| SUBDATE('2008-05-15', INTERVAL 10 DAY) |
+----------------------------------------+
| 2008-05-05                             | 
+----------------------------------------+
1 row in set (0.00 sec)

Example: SUBDATE() function without INTERVAL keyword

The following statement will return a date after subtracting 10 days (notice that INTERVAL keyword is not used) from the specified date 2008-05-15.

Code:


SELECT SUBDATE('2008-05-15', 10);

Output:

mysql> SELECT SUBDATE('2008-05-15', 10);
+---------------------------+
| SUBDATE('2008-05-15', 10) |
+---------------------------+
| 2008-05-05                | 
+---------------------------+
1 row in set (0.00 sec)

Video Presentation:

All Date and Time Functions :

Click here to see the MySQL Date and time functions.

Previous: STR_TO_DATE()
Next: SUBTIME()



Follow us on Facebook and Twitter for latest update.