Subtime() function in MySQL; Through this tutorial, i am going to show you how to subtract date, time, fraction seconds, etc. from given datetime / time value using the MySQL subtime() function.
MySQL SUBTIME() function
The SUBTIME() function subtracts time from a time/datetime expression and then returns the new time/datetime.
Syntax of MySQL SUBTIME() function
The syntax of subtime() function is following:
SUBTIME(expression1, expression2)
Here,
- expression1:- can be either a time or DateTime expression
- expression2 is a time expression.
Example 1 – MySQL SUBTIME() function
Let’s take an example using the MySQL subtime() function; as follows:
SELECT SUBTIME('11:35:00', '1:05');
The output of the above query is:
+--------------------------------+ | SUBTIME('11:35:00', '1:05') | +--------------------------------+ | 10:30:00 | +--------------------------------+
Example 2 – Subtract Seconds In Time Value
Let’s subtract a number of seconds from the time value using the subtime() function; as follows:
SELECT SUBTIME('11:35:00', '1:05:30');
The output of the above query is:
+--------------------------------+ | SUBTIME('11:35:00', '1:05:30') | +--------------------------------+ | 10:29:30 | +--------------------------------+
Example 3 – Subtract Fractional Seconds In Time Value
Let’s subtract a number of fractional seconds from the time value using the subtime() function; as follows:
SELECT SUBTIME('12:35:00.888888', '1:30:30.555555');
The output of the above query is:
+----------------------------------------------+ | SUBTIME('11:40:00.888888', '2:40:20.444444') | +----------------------------------------------+ | 08:59:40.444444 | +----------------------------------------------+
Example 4 – Subtract Time from a DateTime Value
Let’s subtract time value from datetime value using the subtime() function; as follows:
SELECT SUBTIME('2020-01-04 11:40:00', '2:40:40');
The output of the above query is:
+-------------------------------------------+ | SUBTIME('2020-01-04 11:40:00', '2:40:40') | +-------------------------------------------+ | 2020-01-04 08:59:20 | +-------------------------------------------+
Conclusion
Subtime() function in MySQL; Through this tutorial,you have learned how to subtract date, time, fraction seconds, etc. from given datetime / time value using the MySQL subtime() function.