In MySQL, you can use the ADDTIME()
function to add a specified amount of time to a time or datetime expression.
Examples of this function below.
Syntax
First, here’s the syntax:
ADDTIME(expr1,expr2)
So, expr1
is the original date/time value, and expr2
is the amount of time you want to add to it.
Example
Here’s an example to demonstrate.
SELECT ADDTIME('01:00:00', '02:30:00') AS Result;
Result:
+----------+ | Result | +----------+ | 03:30:00 | +----------+
So the first argument is increased by the amount of the second argument.
Fractional Seconds
The time value can have a fractional seconds part if required:
SELECT ADDTIME('01:00:00.000000', '02:30:00.123456') AS Result;
Result:
+-----------------+ | Result | +-----------------+ | 03:30:00.123456 | +-----------------+
Date Values
You can also use it to increment date values:
SELECT ADDTIME('2020-12-01 00:00:00', '20 03:35:59') AS Result;
Result:
+---------------------+ | Result | +---------------------+ | 2020-12-21 03:35:59 | +---------------------+
So in this case we incremented the day, as well as the time component.
Also see DATE_ADD()
and ADDDATE()
for adding a specified time interval to a date value.