If you need to add one or more seconds to a datetime value in MariaDB, here are 8 options to consider.
The DATE_ADD()
Function
The DATE_ADD()
function allows you to add an amount to a date or datetime expression. This includes adding time parts, such as hours, minutes, seconds, etc.
Example:
SELECT DATE_ADD('2021-05-01 10:00:00', INTERVAL 35 SECOND);
Result:
+-----------------------------------------------------+ | DATE_ADD('2021-05-01 10:00:00', INTERVAL 35 SECOND) | +-----------------------------------------------------+ | 2021-05-01 10:00:35 | +-----------------------------------------------------+
In this case, I added 35 seconds to the datetime expression. Note that the SECOND
keyword remains non-plural regardless of whether I’m adding one second or more.
Here’s what happens if I provide just the date:
SELECT DATE_ADD('2021-05-01', INTERVAL 35 SECOND);
Result:
+--------------------------------------------+ | DATE_ADD('2021-05-01', INTERVAL 35 SECOND) | +--------------------------------------------+ | 2021-05-01 00:00:35 | +--------------------------------------------+
The time part is appended to the result, and it is assumed that the initial time was 00:00:00
.
When adding 60 seconds, we can alternatively use the MINUTE
keyword if we prefer:
SELECT DATE_ADD('2021-05-01 10:00:00', INTERVAL 1 MINUTE);
Result:
+----------------------------------------------------+ | DATE_ADD('2021-05-01 10:00:00', INTERVAL 1 MINUTE) | +----------------------------------------------------+ | 2021-05-01 10:01:00 | +----------------------------------------------------+
This technique obviously works with 120 seconds, 180, and so on. Just use the applicable number of minutes.
You can also use a composite date/time unit to add multiple units. For example, if you wanted to add 1 minute and 30 seconds, you could do this:
SELECT
DATE_ADD('2021-05-01 10:00:00', INTERVAL '1:30' MINUTE_SECOND);
Result:
+----------------------------------------------------------------+ | DATE_ADD('2021-05-01 10:00:00', INTERVAL '1:30' MINUTE_SECOND) | +----------------------------------------------------------------+ | 2021-05-01 10:01:30 | +----------------------------------------------------------------+
See MariaDB Date and Time Units for a full list of date/time units.
The ADDDATE()
Function
Another way to add seconds to a datetime expression is to use the ADDDATE()
function. This function has two syntaxes:
- One syntax allows you to add a number of days to a date, which is not what we’re doing here (unless we plan to add seconds in blocks of 86,400, which is how many seconds are in a day).
- The other syntax is the same as with the
DATE_ADD()
function above. When using this syntax,ADDDATE()
is a synonym forDATE_ADD()
.
Example:
SELECT ADDDATE('2021-05-01 10:00:00', INTERVAL 35 SECOND);
Result:
+----------------------------------------------------+ | ADDDATE('2021-05-01 10:00:00', INTERVAL 35 SECOND) | +----------------------------------------------------+ | 2021-05-01 10:00:35 | +----------------------------------------------------+
The DATE_SUB()
Function
The DATE_SUB()
function allows you to subtract an amount to a date/datetime expression. However, if you provide a negative value to subtract, then it ends up being added to the date/time.
Example:
SELECT DATE_SUB('2021-05-01 10:00:00', INTERVAL -35 SECOND);
Result:
+------------------------------------------------------+ | DATE_SUB('2021-05-01 10:00:00', INTERVAL -35 SECOND) | +------------------------------------------------------+ | 2021-05-01 10:00:35 | +------------------------------------------------------+
The SUBDATE()
Function
The SUBDATE()
function is a synonym for DATE_SUB()
when using the following syntax.
Example:
SELECT SUBDATE('2021-05-01 10:00:00', INTERVAL -35 SECOND);
Result:
+-----------------------------------------------------+ | SUBDATE('2021-05-01 10:00:00', INTERVAL -35 SECOND) | +-----------------------------------------------------+ | 2021-05-01 10:00:35 | +-----------------------------------------------------+
The +
Operator
Another option is to use the +
operator.
The +
operator is used to perform addition, and we can use it on dates, along with the applicable date/time unit to add a certain number of seconds to our datetime expression.
Example:
SELECT '2021-05-01 10:00:00' + INTERVAL 35 SECOND;
Result:
+--------------------------------------------+ | '2021-05-01 10:00:00' + INTERVAL 35 SECOND | +--------------------------------------------+ | 2021-05-01 10:00:35 | +--------------------------------------------+
The -
Operator
The -
operator can be used to subtract an amount from a date. But if it’s used to subtract a negative number, then the result is a positive amount being added to the datetime expression:
SELECT '2021-05-01 10:00:00' - INTERVAL -35 SECOND;
Result:
+---------------------------------------------+ | '2021-05-01 10:00:00' - INTERVAL -35 SECOND | +---------------------------------------------+ | 2021-05-01 10:00:35 | +---------------------------------------------+
The ADDTIME()
Function
You can use the ADDTIME()
function to add a number of seconds to a datetime expression.
Example:
SELECT ADDTIME('2021-05-01 10:00:00', '00:00:30');
Result:
+--------------------------------------------+ | ADDTIME('2021-05-01 10:00:00', '00:00:30') | +--------------------------------------------+ | 2021-05-01 10:00:30 | +--------------------------------------------+
One benefit of this function is that you can also change the other time units, such as hours, minutes, etc.
The SUBTIME()
Function
You can alternatively switch it around and use SUBTIME()
with a negative value if you so wish:
SELECT SUBTIME('2021-05-01 10:00:00', '-00:00:30');
Result:
+---------------------------------------------+ | SUBTIME('2021-05-01 10:00:00', '-00:00:30') | +---------------------------------------------+ | 2021-05-01 10:00:30 | +---------------------------------------------+
Other Date and Time Units
The above examples add a number of seconds to the datetime value, but we can use the same techniques to add any date/time unit. See MariaDB Date and Time Units for a full list of date/time units that can be used with the above functions and operators.