How DATE_ADD() Works in MariaDB

In MariaDB, DATE_ADD() is a built-in date and time function that performs date arithmetic.

It allows you to change a date by specifying the date, the unit to add, and the amount to add. You can pass a negative amount if you need to subtract the date by a certain interval.

Syntax

The syntax goes like this:

DATE_ADD(date,INTERVAL expr unit)

Where date is the date to change, expr is the amount to add, and unit is the date/time unit to add (e.g. second, minute, etc).

Example

Here’s a basic example:

SELECT DATE_ADD('2021-05-31 10:00:00', INTERVAL 1 HOUR);

Result:

+--------------------------------------------------+
| DATE_ADD('2021-05-31 10:00:00', INTERVAL 1 HOUR) |
+--------------------------------------------------+
| 2021-05-31 11:00:00                              |
+--------------------------------------------------+

This is like doing the following:

SELECT '2021-05-31 10:00:00' + INTERVAL 1 HOUR;

Result:

+-----------------------------------------+
| '2021-05-31 10:00:00' + INTERVAL 1 HOUR |
+-----------------------------------------+
| 2021-05-31 11:00:00                     |
+-----------------------------------------+

Negative Intervals

Providing a negative interval subtracts that amount from the date.

Example:

SELECT DATE_ADD('2021-05-31 10:00:00', INTERVAL -1 HOUR);

Result:

+---------------------------------------------------+
| DATE_ADD('2021-05-31 10:00:00', INTERVAL -1 HOUR) |
+---------------------------------------------------+
| 2021-05-31 09:00:00                               |
+---------------------------------------------------+

Other Units

Here’s an example that adds an interval of 1 to the various date and time units:

SELECT 
    DATE_ADD('2021-05-01 10:00:00', INTERVAL 1 YEAR) AS YEAR,
    DATE_ADD('2021-05-01 10:00:00', INTERVAL 1 MONTH) AS MONTH,
    DATE_ADD('2021-05-01 10:00:00', INTERVAL 1 DAY) AS DAY,
    DATE_ADD('2021-05-01 10:00:00', INTERVAL 1 HOUR) AS HOUR,
    DATE_ADD('2021-05-01 10:00:00', INTERVAL 1 MINUTE) AS MINUTE,
    DATE_ADD('2021-05-01 10:00:00', INTERVAL 1 SECOND) AS SECOND,
    DATE_ADD('2021-05-01 10:00:00', INTERVAL 1 MICROSECOND) AS MICROSECOND;

Result (using vertical output):

       YEAR: 2022-05-01 10:00:00
      MONTH: 2021-06-01 10:00:00
        DAY: 2021-05-02 10:00:00
       HOUR: 2021-05-01 11:00:00
     MINUTE: 2021-05-01 10:01:00
     SECOND: 2021-05-01 10:00:01
MICROSECOND: 2021-05-01 10:00:00.000001

Composite Units

Here’s an example that uses composite units:

SELECT 
    DATE_ADD('2021-05-01 10:00:00', INTERVAL '1:2' YEAR_MONTH) AS "YEAR_MONTH",
    DATE_ADD('2021-05-01 10:00:00', INTERVAL '1:25:35' HOUR_SECOND) AS "HOUR_SECOND",
    DATE_ADD('2021-05-01 10:00:00', INTERVAL '1:30' DAY_MINUTE) AS "DAY_MINUTE";

Result:

+---------------------+---------------------+---------------------+
| YEAR_MONTH          | HOUR_SECOND         | DAY_MINUTE          |
+---------------------+---------------------+---------------------+
| 2022-07-01 10:00:00 | 2021-05-01 11:25:35 | 2021-05-01 11:30:00 |
+---------------------+---------------------+---------------------+

Null Dates

Passing null for the date returns null:

SELECT DATE_ADD(null, INTERVAL 1 YEAR);

Result:

+---------------------------------+
| DATE_ADD(null, INTERVAL 1 YEAR) |
+---------------------------------+
| NULL                            |
+---------------------------------+

Missing Argument

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

SELECT DATE_ADD();

Result:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1