This tutorial explains MySQL DATE_SUB function which subtracts a slice of time (in days/hours, e.g., 1 day or 10 days) from the given date. We’ll describe the complete date arithmetic of this method with the help of simple examples.
Let’s now go through each of the sections one by one.
MySQL DATE_SUB() Function
As stated initially, DATE_SUB() is a built-in MySQL function that subtracts the specified number of days from a given date. So, let’s now see the details and check out how can we use it.
Below is the signature of this method:
DATE_SUB(Given_date, INTERVAL expr unit);
Below are the descriptions of the parameters in the above function.
+-------------+-----------------------------------------+ | # Params | # Description | +-------------+-----------------------------------------+ | Given_date | Date value of DATE or DATETIME type. | | expr | String representing time interval value.| +-------------+-----------------------------------------+
Let’s take an example that demonstrates the DATE_SUB() function to deduct 2 days from the current date.
SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) result;
In the above statement, we’ve called the MySQL CURDATE() function which returns the date of the present day. Anyways, after executing this command, it gives the following output:
1 MySQL Workbench 2019-07-07
The current date was “2019-07-09” at the time of execution. Hence, after subtracting two days, the result came out as above.
After looking at the arguments, let’s check out what does the DATE_SUB() function returns.
- The return value would also be a DATE when the input value was of the DATE type.
- It would return a DATETIME if the input had time in hours, minutes, and seconds.
- A string value is sent back in all other cases.
Below is an example that calls the DATE_SUB() function to subtract in terms of hours.
SELECT DATE_SUB(CURDATE(), INTERVAL 6 HOUR) result;
After executing the above MySQL command, the result comes as:
1 MySQL Workbench 2019-07-08 18:00:00
Let’s now unveil several examples addressing different situations.
The second argument (expr) of the DATE_SUB() function can also have -ve interval values.
However, if we pass a negative interval value, then it will act like the DATE_ADD() function. You will get more clarity by seeing the below example.
SELECT DATE_SUB(CURDATE(), INTERVAL -2 day) result;
Since the current date was “2019-07-09” at the time of execution, so the above command added two days to the final result. Check the result/outcome below.
1 MySQL Workbench 2019-07-11
Also Read: MySQL ABS Function with Simple Examples
It is a negative case when you pass an invalid, malformed, or NULL date value to the DATE_SUB(). This function responds to such inputs by returning a NULL.
SELECT DATE_SUB('2019-13-09', INTERVAL 2 day) result;
The date “2019-13-09” is an incorrect value as the month can’t go beyond 12. Hence, after executing the above command, the output will be:
1 MySQL Workbench NULL
The MySQL system would also notice this operation and throw a warning. We can track the same by issuing the below command:
Warning 1292 Incorrect datetime value: '2019-13-09'
Now, let’s have one more example to observe the behavior of the MySQL DATE_SUB() function for a malformed date.
-- Input date is malformed SELECT DATE_SUB('2019-07-09-2019', INTERVAL 2 day) result; -- Input date is NULL SELECT DATE_SUB(NULL, INTERVAL 2 day) result;
After running the above MySQL statements, you should see the following outcome:
1 MySQL Workbench NULL NULL
The DATE_SUB() function automatically adjusts the output in the following situation:
- After subtracting the interval (MONTH, YEAR, or YEAR_MONTH), the result comes to a date that surpasses the maximum day of the new month.
In this scenario, the function normalizes the final date to the last day of the new month. You can observe yourself by running through the example below:
-- Passing '2019-07-31' as the edge date value SELECT DATE_SUB('2019-07-31', INTERVAL 1 MONTH) result;
After execution, we get this:
1 MySQL Workbench 2019-06-30
In this example, we backtracked by 1 month from July 31st, 2019. Therefore, the output comes to be June 30th, 2019. The day was automatically normalized to June, 30th instead of July, 1st.
We hope that after wrapping up this tutorial, you will feel comfortable using the MySQL DATE_SUB() function. However, you may practice more with examples to gain confidence.
Also, to learn SQL from scratch to depth, read our step-by-step MySQL tutorial.