MySQL numeric functions
MySQL string functions
MySQL Date/Time functions
MySQL aggregate functions
MySQL flow control functions
The MySQL ADDTIME function is a date and time function used to add a specified time value to another time or date-time value.
Here's a tutorial on using the MySQL ADDTIME function:
1. Syntax
The syntax for the ADDTIME function is:
ADDTIME(time1, time2)
Where time1
and time2
are the time or date-time values you want to add. The function returns the result as a time or date-time value.
2. Examples
Let's start with some basic examples of the ADDTIME function.
a. Adding two time values
SELECT ADDTIME('10:30:45', '02:15:30');
Result:
12:46:15
b. Adding time value to a date-time value
SELECT ADDTIME('2022-08-15 10:30:00', '01:30:00');
Result:
2022-08-15 12:00:00
c. Adding a negative time value
SELECT ADDTIME('10:30:45', '-01:30:00');
Result:
09:00:45
3. Using ADDTIME function in a table
Let's assume we have the following events
table:
+----+---------------------+------------+ | id | start_time | duration | +----+---------------------+------------+ | 1 | 2023-05-10 10:30:00 | 02:00:00 | | 2 | 2023-05-11 15:45:00 | 01:30:00 | | 3 | 2023-05-12 12:00:00 | 00:45:00 | | 4 | 2023-05-13 17:30:00 | 01:15:00 | +----+---------------------+------------+
a. Calculate the end time for each event
SELECT id, start_time, ADDTIME(start_time, duration) AS end_time FROM events;
Result:
+----+---------------------+---------------------+ | id | start_time | end_time | +----+---------------------+---------------------+ | 1 | 2023-05-10 10:30:00 | 2023-05-10 12:30:00 | | 2 | 2023-05-11 15:45:00 | 2023-05-11 17:15:00 | | 3 | 2023-05-12 12:00:00 | 2023-05-12 12:45:00 | | 4 | 2023-05-13 17:30:00 | 2023-05-13 18:45:00 | +----+---------------------+---------------------+
In this tutorial, you learned how to use the MySQL ADDTIME function to add time or date-time values, both for individual values and within table data. This function can be useful when you need to perform calculations with time and date-time values in your database queries.
How to use ADDTIME function in MySQL:
SELECT ADDTIME('09:30:00', '03:15:30') AS result_time;
MySQL ADDTIME function examples:
SELECT ADDTIME('12:30:00', '04:45:15') AS result1, ADDTIME('23:45:30', '01:15:45') AS result2;
Time addition in MySQL using ADDTIME:
SELECT ADDTIME(column1, column2) AS total_time FROM table_name;
ADDTIME function in MySQL for time intervals:
SELECT ADDTIME('08:00:00', '1:30:00') AS new_time;
Performing time calculations with ADDTIME in MySQL:
SELECT ADDTIME(column1, '04:30:00') AS new_time FROM table_name;
ADDTIME function in MySQL for date and time values:
SELECT ADDTIME('2023-01-15 09:00:00', '03:30:00') AS new_datetime;
Use cases for ADDTIME function in MySQL queries:
SELECT * FROM appointments WHERE ADDTIME(start_time, '01:00:00') > NOW();
ADDTIME function in MySQL for positive and negative time:
SELECT ADDTIME('12:00:00', '-01:30:00') AS updated_time;
Mathematical operations with ADDTIME in MySQL:
SELECT ADDTIME(column1 * 2, '03:15:00') AS modified_time FROM table_name;
MySQL ADDTIME function in WHERE clause:
SELECT * FROM tasks WHERE ADDTIME(completion_time, '02:00:00') > deadline_time;
ADDTIME function in MySQL for timestamp values:
SELECT ADDTIME('2023-01-15 14:30:00', '05:45:00') AS new_timestamp;