MySQL - ADDTIME() Function


The DATE, DATETIME and TIMESTAMP datatypes in MySQL are used to store the date, date and time, time stamp values respectively. Where a time stamp is a numerical value representing the number of milliseconds from '1970-01-01 00:00:01' UTC (epoch) to the specified time. MySQL provides a set of functions to manipulate these values.

The MYSQL ADDTIME() function is used to add the specified time interval to a date time or, time value.

Syntax

Following is the syntax of the above function –

ADDTIME(expr1,expr2)

where,

  • expr1 is the expression representing the datetime or time.

  • expr2 is the expression representing the time interval to be added.

Example 1

Following example demonstrates the usage of the ADDTIME() function –

mysql> SELECT ADDTIME('10:40:32.88558', '06:04:01.222222');
+----------------------------------------------+
| ADDTIME('10:40:32.88558', '06:04:01.222222') |
+----------------------------------------------+
|                              16:44:34.107802 |
+----------------------------------------------+
1 row in set (0.00 sec)

Example 2

Following is another example of this function –

mysql> SELECT ADDTIME('06:23:15.99999', '12:25:11.11111');
+---------------------------------------------+
| ADDTIME('06:23:15.99999', '12:25:11.11111') |
+---------------------------------------------+
|                             18:48:27.111100 |
+---------------------------------------------+
1 row in set (0.00 sec)

Example 3

In the following example we are passing DATETIME value for time –

mysql> SELECT ADDTIME('2018-05-23 05:40:32.88558', '06:04:01.222222');
+---------------------------------------------------------+
| ADDTIME('2018-05-23 05:40:32.88558', '06:04:01.222222') |
+---------------------------------------------------------+
|                              2018-05-23 11:44:34.107802 |
+---------------------------------------------------------+
1 row in set (0.00 sec)

Example 4

In the following example we are passing the result of the CURTIME() function as the time interval —

mysql> SELECT ADDTIME('2018-05-23 05:40:32.88558', CURTIME());
+-------------------------------------------------+
| ADDTIME('2018-05-23 05:40:32.88558', CURTIME()) |
+-------------------------------------------------+
|                      2018-05-23 17:58:41.885580 |
+-------------------------------------------------+
1 row in set (0.00 sec)

Example 5

We can also pass negative values as arguments to this function –

mysql> SELECT ADDTIME('2018-05-23 05:40:32.88558', '-06:04:01.222222');
+----------------------------------------------------------+
| ADDTIME('2018-05-23 05:40:32.88558', '-06:04:01.222222') |
+----------------------------------------------------------+
|                               2018-05-22 23:36:31.663358 |
+----------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT ADDTIME('06:23:15.99999', '-02:25:11.11111');
+----------------------------------------------+
| ADDTIME('06:23:15.99999', '-02:25:11.11111') |
+----------------------------------------------+
|                              03:58:04.888880 |
+----------------------------------------------+
1 row in set (0.00 sec)

Example 6

Let us create another table with name Sales in MySQL database using CREATE statement as follows –

mysql> CREATE TABLE sales(
	ID INT,
	ProductName VARCHAR(255),
	CustomerName VARCHAR(255),
	DispatchDate date,
	DispatchTime time,
	Price INT,
	Location VARCHAR(255)
);
Query OK, 0 rows affected (2.22 sec)

Now, we will insert 5 records in Sales table using INSERT statements −

insert into sales values (1, 'Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad');
insert into sales values (2, 'Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam');
insert into sales values (3, 'Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada');
insert into sales values (4, 'Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai');
insert into sales values (5, 'Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');

Following query adds time interval to the values in the column named DispatchTime —

mysql> SELECT ProductName, CustomerName, DispatchDate, DispatchTime, Price, ADDTIME(DispatchTime,'12:45:50') FROM Sales;
+-------------+--------------+--------------+--------------+-------+----------------------------------+
| ProductName | CustomerName | DispatchDate | DispatchTime | Price | ADDTIME(DispatchTime,'12:45:50') |
+-------------+--------------+--------------+--------------+-------+----------------------------------+
|   Key-Board |         Raja |   2019-09-01 |     11:00:00 |  7000 |                         23:45:50 |
|   Earphones |         Roja |   2019-05-01 |     11:00:00 |  2000 |                         23:45:50 |
|       Mouse |         Puja |   2019-03-01 |     10:59:59 |  3000 |                         23:45:49 |
|      Mobile |       Vanaja |   2019-03-01 |     10:10:52 |  9000 |                         22:56:42 |
|     Headset |       Jalaja |   2019-04-06 |     11:08:59 |  6000 |                         23:54:49 |
+-------------+--------------+--------------+--------------+-------+----------------------------------+
5 rows in set (0.00 sec)

Example 7

Suppose we have created a table named dispatches_data with 5 records in it using the following queries –

mysql> CREATE TABLE dispatches_data(
	ProductName VARCHAR(255),
	CustomerName VARCHAR(255),
	DispatchTimeStamp timestamp,
	Price INT,
	Location VARCHAR(255)
);
insert into dispatches_data values('Key-Board', 'Raja', TIMESTAMP('2019-05-04', '15:02:45'), 7000, 'Hyderabad');
insert into dispatches_data values('Earphones', 'Roja', TIMESTAMP('2019-06-26', '14:13:12'), 2000, 'Vishakhapatnam');
insert into dispatches_data values('Mouse', 'Puja', TIMESTAMP('2019-12-07', '07:50:37'), 3000, 'Vijayawada');
insert into dispatches_data values('Mobile', 'Vanaja' , TIMESTAMP ('2018-03-21', '16:00:45'), 9000, 'Chennai');
insert into dispatches_data values('Headset', 'Jalaja' , TIMESTAMP('2018-12-30', '10:49:27'), 6000, 'Goa');

Following query adds time interval to the column named DispatchTimeStamp —

mysql> SELECT ProductName, CustomerName, DispatchTimeStamp, Price, ADDTIME(DispatchTimeStamp, '08:25:46') FROM dispatches_data;
+-------------+--------------+---------------------+-------+----------------------------------------+
| ProductName | CustomerName |   DispatchTimeStamp | Price | ADDTIME(DispatchTimeStamp, '08:25:46') |
+-------------+--------------+---------------------+-------+----------------------------------------+
|   Key-Board |         Raja | 2019-05-04 15:02:45 |  7000 |                    2019-05-04 23:28:31 |
|   Earphones |         Roja | 2019-06-26 14:13:12 |  2000 |                    2019-06-26 22:38:58 |
|       Mouse |         Puja | 2019-12-07 07:50:37 |  3000 |                    2019-12-07 16:16:23 |
|      Mobile |       Vanaja | 2018-03-21 16:00:45 |  9000 |                    2018-03-22 00:26:31 |
|     Headset |       Jalaja | 2018-12-30 10:49:27 |  6000 |                    2018-12-30 19:15:13 |
+-------------+--------------+---------------------+-------+----------------------------------------+
5 rows in set (0.21 sec)

Example 8

Suppose we have created a table named SubscriberDetails with 5 records in it using the following queries –

mysql> CREATE TABLE SubscriberDetails (
	SubscriberName VARCHAR(255),
	PackageName VARCHAR(255),
	SubscriptionTimeStamp timestamp
);
insert into SubscriberDetails values('Ram', 'Premium', TimeStamp('2020-10-21 20:53:49'));
insert into SubscriberDetails values('Rahman', 'Basic', TimeStamp('2020-11-26 10:13:19'));
insert into SubscriberDetails values('Robert', 'Moderate', TimeStamp('2021-03-07 05:43:20'));
insert into SubscriberDetails values('Radha', 'Basic', TimeStamp('2021-02-21 16:36:39'));
insert into SubscriberDetails values('Rajiya', 'Premium', TimeStamp('2021-01-30 12:45:45'));

Following query adds time interval to the SubscriptionTimeStamp values of all the records –

mysql> SELECT SubscriberName, PackageName, SubscriptionTimeStamp, ADDTIME(SubscriptionTimeStamp, '10:05:20') FROM SubscriberDetails;
+----------------+-------------+-----------------------+--------------------------------------------+
| SubscriberName | PackageName | SubscriptionTimeStamp | ADDTIME(SubscriptionTimeStamp, '10:05:20') |
+----------------+-------------+-----------------------+--------------------------------------------+
|            Ram |     Premium |   2020-10-21 20:53:49 |                        2020-10-22 06:59:09 |
|         Rahman |       Basic |   2020-11-26 10:13:19 |                        2020-11-26 20:18:39 |
|         Robert |    Moderate |   2021-03-07 05:43:20 |                        2021-03-07 15:48:40 |
|          Radha |       Basic |   2021-02-21 16:36:39 |                        2021-02-22 02:41:59 |
|         Rajiya |     Premium |   2021-01-30 12:45:45 |                        2021-01-30 22:51:05 |
+----------------+-------------+-----------------------+--------------------------------------------+
5 rows in set (0.00 sec)
mysql-date-time-functions.htm
Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements