The ADDTIME() function is used in MySQL to add a specific time interval to a given time or datetime and return the resulting time/datetime.
Definition and Usage
- The
ADDTIME()function allows you to add an interval (e.g., seconds, minutes, hours, days) to a time or datetime value.
Syntax
ADDTIME(datetime, addtime)
Parameter Values
| Parameter | Description |
|---|---|
datetime |
The time or datetime value to which the interval will be added. |
addtime |
The time interval to add (e.g., "2", "2:10:5", "5 2:10:5.000003"). Both positive and negative values are allowed. |
Technical Details
- Works in: From MySQL 4.0
- Return Value: A time or datetime value after adding the specified interval.
Examples
Example 1: Add 2 seconds to a datetime
SELECT ADDTIME("2017-06-15 09:34:21", "2");
Output:
ADDTIME("2017-06-15 09:34:21", "2")
-----------------------------------
2017-06-15 09:34:23
Here, 2 seconds are added to the datetime 2017-06-15 09:34:21, resulting in 2017-06-15 09:34:23.
Example 2: Add 5 seconds and 3 microseconds to a datetime
SELECT ADDTIME("2017-06-15 09:34:21.000001", "5.000003");
Output:
ADDTIME("2017-06-15 09:34:21.000001", "5.000003")
-------------------------------------------------
2017-06-15 09:34:26.000004
Here, 5 seconds and 3 microseconds are added to the datetime 2017-06-15 09:34:21.000001, resulting in 2017-06-15 09:34:26.000004.
Example 3: Add 2 hours, 10 minutes, 5 seconds, and 3 microseconds to a datetime
SELECT ADDTIME("2017-06-15 09:34:21.000001", "2:10:5.000003");
Output:
ADDTIME("2017-06-15 09:34:21.000001", "2:10:5.000003")
------------------------------------------------------
2017-06-15 11:44:26.000004
In this case, 2 hours, 10 minutes, 5 seconds, and 3 microseconds are added, resulting in 2017-06-15 11:44:26.000004.
Example 4: Add 5 days, 2 hours, 10 minutes, 5 seconds, and 3 microseconds to a datetime
SELECT ADDTIME("2017-06-15 09:34:21.000001", "5 2:10:5.000003");
Output:
ADDTIME("2017-06-15 09:34:21.000001", "5 2:10:5.000003")
---------------------------------------------------------
2017-06-20 11:44:26.000004
Here, 5 days, 2 hours, 10 minutes, 5 seconds, and 3 microseconds are added to the datetime, giving 2017-06-20 11:44:26.000004.
Example 5: Add 2 hours, 10 minutes, 5 seconds, and 3 microseconds to a time
SELECT ADDTIME("09:34:21.000001", "2:10:5.000003");
Output:
ADDTIME("09:34:21.000001", "2:10:5.000003")
-------------------------------------------
11:44:26.000004
In this example, 2 hours, 10 minutes, 5 seconds, and 3 microseconds are added to the time 09:34:21.000001, resulting in 11:44:26.000004.
Related Functions
SUBTIME(): Subtracts a time interval from a time/datetime.DATE_ADD(): Adds a time/date interval to a date.TIMESTAMPADD(): Adds an interval to a timestamp.
Use Cases
- Time Calculations: Adding specific time intervals (like hours, minutes, seconds) to a given time.
- Scheduling: Automatically calculating future times, such as due times, expiry times, or deadlines.