Open In App

MySQL DATE_SUB() Function

Last Updated : 06 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The MySQL DATE_SUB() function subtracts a specified time or date interval from a datetime value.

DATE_SUB() Function in MySQL

The DATE_SUB() Function in MySQL allows for various date and time calculations by subtracting time intervals. It is used to subtract a specified time or date interval from a given date and then return the resulting date.

Syntax

MySQL DATE_SUB() function syntax is:

DATE_SUB(date, INTERVAL value addunit)

Parameter:

This function accepts two parameters which are illustrated below :

  • date – Specified date to be modified
  • value addunit – Here the value is a date or time interval to subtract. This value can be both positive and negative. Here the addunit is the type of interval to subtract such as SECOND, MINUTE, HOUR, DAY, YEAR, MONTH, etc.

MySQL DATE_SUB() Function Example

Let us look at some examples of the DATE_SUB function in MySQL . Learning MySQL DATE_SUB() function with examples will help in understanding it better.

Example 1

Getting a new date of “2017-11-22” after the subtraction of 3 year to the specified date “2020-11-22”.

SELECT DATE_SUB("2020-11-22", INTERVAL 3 YEAR);

Output :

2017-11-22

Example 2

Getting a new date of “2020-9-22” after the subtraction of 2 month to the specified date “2020-11-22”.

SELECT DATE_SUB("2020-11-22", INTERVAL 2 MONTH);

Output :

2020-09-22

Example 3

Getting a new date of “2020-11-12” after the subtraction of 10 days to the specified date “2020-11-22”.

SELECT DATE_SUB("2020-11-22", INTERVAL 10 DAY);

Output :

2020-11-12

Example 4

Getting a new date of “2020-11-22 06:12:10” after the subtraction of 3 hours to the specified date “2020-11-22 09:12:10”.

SELECT DATE_SUB("2020-11-22 09:12:10", INTERVAL 3 HOUR);

Output :

2020-11-22 06:12:10

Important Points About MySQL DATE_SUB() Function

  • The MySQL DATE_SUB() function is used to subtract a specified time or date interval from a given date and then returns the resulting date.
  • The DATE_SUB() function is useful for performing date arithmetic, calculating past datetime values, and tracking durations.
  • The function can be used with both date and datetime values.
  • Related functions include DATE(), LAST_DAY(), DATE_FORMAT(), DATE_ADD(), and DATEDIFF().


Next Article
Article Tags :

Similar Reads