MySQL ADDTIME() Function

Summary: in this tutorial, you will learn how to use the MySQL ADDTIME() function to add a time to another.

Introduction to MySQL ADDTIME() function

The ADDTIME() function allows you to add a time interval to a time or datetime expression. Here’s the syntax of the ADDTIME() function:

ADDTIME(datetime, time_interval)Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • datetime: The initial datetime or time value you want to add the time interval.
  • time_interval: The time interval you want to add, specified as a time expression in the format ‘HH:MM:SS‘.

In practice, you often use the ADDTIME() function to add hours, minutes, or seconds to a timestamp.

MySQL ADDTIME() function examples

Let’s take some examples of using the ADDTIME() function.

1) Basic MySQL ADDTIME() function example

The following example uses the ADDTIME() function to add 3 hours, 30 minutes, and 45 seconds to the date time ‘2023-10-23 14:30:00’:

SELECT ADDTIME('2023-10-23 14:30:00', '03:30:45');Code language: SQL (Structured Query Language) (sql)

Output:

+--------------------------------------------+
| ADDTIME('2023-10-23 14:30:00', '03:30:45') |
+--------------------------------------------+
| 2023-10-23 18:00:45                        |
+--------------------------------------------+
1 row in set (0.01 sec)Code language: SQL (Structured Query Language) (sql)

2) Adding Minutes to a Time Value

The following example uses the ADDTIME() function to add 15 minutes to a time value that represents the duration of a meeting:

SELECT ADDTIME('00:30:00', '00:15:00');Code language: SQL (Structured Query Language) (sql)

Output:

+---------------------------------+
| ADDTIME('00:30:00', '00:15:00') |
+---------------------------------+
| 00:45:00                        |
+---------------------------------+
1 row in set (0.00 sec)Code language: SQL (Structured Query Language) (sql)

The result is ’00:45:00′, which represents the updated meeting duration.

3) Using the ADDTIME() function with negative time intervals

The ADDTIME() function allows you to subtract a time by using negative time intervals.

For example, if you have a time value representing the duration of a break and you want to subtract 10 minutes from it:

SELECT ADDTIME('00:15:00', '-00:10:00');Code language: SQL (Structured Query Language) (sql)

Output:

+----------------------------------+
| ADDTIME('00:15:00', '-00:10:00') |
+----------------------------------+
| 00:05:00                         |
+----------------------------------+
1 row in set (0.00 secCode language: SQL (Structured Query Language) (sql)

The result is ’00:05:00′ which indicates the new duration after subtracting 10 minutes.

Summary

  • Use the ADDTIME() function to add a time interval to a time or datetime value.
Was this tutorial helpful?