How SUBTIME() Works in MariaDB

In MariaDB, SUBTIME() is a built-in date and time function that subtracts an amount from a time or datetime expression.

It allows you to change a time or datetime expression by passing that expression, as well as a time expression to subtract from that expression. You can pass a negative amount if you need to add to the time expression.

Syntax

The syntax goes like this:

SUBTIME(expr1,expr2)

Where expr1 is a time or datetime expression, and expr2 is a time expression.

Example

Here’s an example:

SELECT SUBTIME('10:00:00', '02:30:45');

Result:

+---------------------------------+
| SUBTIME('10:00:00', '02:30:45') |
+---------------------------------+
| 07:29:15                        |
+---------------------------------+

Datetime Expressions

The first argument can also be a datetime expression:

SELECT SUBTIME('2021-05-01 10:00:00', '22:30:45');

Result:

+--------------------------------------------+
| SUBTIME('2021-05-01 10:00:00', '22:30:45') |
+--------------------------------------------+
| 2021-04-30 11:29:15                        |
+--------------------------------------------+

In this case, enough time was subtracted to pull the date back to the previous day and month.

Time Expressions in a Looser Format

MariaDB allows assignment of times in looser formats, such as dropping leading zeros and providing a value for the day.

Therefore, we can do this:

SELECT SUBTIME('2021-05-01 10:00:00', '366 1:1:1.123456');

Result:

+----------------------------------------------------+
| SUBTIME('2021-05-01 10:00:00', '366 1:1:1.123456') |
+----------------------------------------------------+
| 2020-04-30 08:58:58.876544                         |
+----------------------------------------------------+

Negative Intervals

Providing a negative interval for the day portion adds that amount to the time/datetime.

Example:

SELECT SUBTIME('2021-05-01 10:00:00', '-366 1:1:1.123456');

Result:

+-----------------------------------------------------+
| SUBTIME('2021-05-01 10:00:00', '-366 1:1:1.123456') |
+-----------------------------------------------------+
| 2022-05-02 11:01:01.123456                          |
+-----------------------------------------------------+

Null Arguments

If any argument is null, the result is null:

SELECT 
    SUBTIME('10:00:00', null),
    SUBTIME(null, '02:30:45'),
    SUBTIME(null, null);

Result:

+---------------------------+---------------------------+---------------------+
| SUBTIME('10:00:00', null) | SUBTIME(null, '02:30:45') | SUBTIME(null, null) |
+---------------------------+---------------------------+---------------------+
| NULL                      | NULL                      | NULL                |
+---------------------------+---------------------------+---------------------+

Missing Argument

Calling SUBTIME() with the wrong number of arguments, or without passing any arguments results in an error:

SELECT SUBTIME();

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'SUBTIME'