MySQL: SUBTIME() Function Examples

MySQL: SUBTIME() Function Examples

subtract time in MySQL, You can use the MySQL subtime() function to subtract date, time, fraction seconds, etc. from given datetime / time value. This function accepts two arguments, the first one is DateTime or Time and the second one is time value.

In MySQL SUBTIME() function tutorial, we would love to share with MySQL SUBTIME() function with its syntax, definition, parameters, and several examples.

MySQL SUBTIME() function

Definition:– SUBTIME() function is built-in MySQL function, which is commonly used to subtract a time value from a time or datetime value or exp.

Here, exp is expression.

Syntax

The syntax of subtime() function is following:

 SUBTIME(expression1,  expression2)

Parameters of subtime() function is following:

  • expression1:- can be either a time or DateTime expression
  • expression2 is a time expression.

Example 1 – Simple Usage

Here, We will take an example to demonstrate substime() function.

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 – Subtracting Seconds In Time Value

Here, we will take a second example, we also subtract a number of seconds from the time value.

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

Here, we will take a third example, we also subtract a number of fractional seconds from the time value.

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 from a DateTime Value

Here, we will take the fourth example of subtime function, in this example, we subtract DateTime value.

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                       |
 +-------------------------------------------+

Recommended Tutorial

  1. MySQL Tutorial With Examples
  2. PHP Date and Time functions tutorial
  3. MySQL: DATE() And TIME() Functions Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *