Skip to content
Related Articles

Related Articles

Improve Article
ADDTIME() function in MySQL
  • Last Updated : 16 Nov, 2020

ADDTIME() function in MySQL is used to add the specified time intervals to the given date and time. It returns the date or DateTime after adding the time interval.

Syntax :

ADDTIME(expr1, expr2)

Parameter : This method accepts two parameter.

  • expr1 : The given datetime or time which we want to modify.
  • expr2 : The time interval which we want to add to given datetime. It can be both positive and negative.

Returns : It returns the date or DateTime after adding the given time interval.

Example-1 :
Adding 15 seconds with the specified time using ADDTIME Function.



SELECT ADDTIME("11:34:21", "15") as Updated_time ;

Output :

Updated_time
11:34:36

Example-2 :
Adding 10 minutes with the specified time using ADDTIME Function.

SELECT ADDTIME("10:54:21", "00:10:00") 
as Updated_time ;

Output :

Updated_time
11:04:21

Example-3 :
Adding 12 hours with the specified datetime using ADDTIME Function.

SELECT ADDTIME("2009-02-20 18:04:22.333444", "12:00:00") 
as Updated_time ;

Output :

Updated_time
 2009-02-21 06:04:22.333444

Example-4 :
Adding 10 hours 30 minute 25 second and 100000 Microseconds with the specified datetime using ADDTIME Function.

SELECT ADDTIME("2020-09-20 17:04:22.222333", "10:30:25.100000") 
as Updated_time ;

Output :

Updated_time
2020-09-21 03:34:47.322333

Example-5 :
The ADDTIME function can be used to set value of columns. To demonstrate create a table named ScheduleDetails

CREATE TABLE ScheduleDetails(
TrainId INT NOT NULL,
StationName VARCHAR(20) NOT NULL,
TrainName VARCHAR(20) NOT NULL,
ScheduledlArrivalTime TIME NOT NULL,
PRIMARY KEY(TrainId )
);

Now inserting values in ScheduleDetails table. We will use ADDTIME function which will denote delay in arrival timing. The value in ExpectedArrivalTime column will be the value given by ADDTIME Function.

INSERT INTO  
ScheduleDetails (TrainId, StationName, TrainName, ScheduledlArrivalTime )
VALUES
(12345, 'NJP', 'Saraighat Express', "17:04:22");

Now, checking the ScheduleDetails table :

SELECT *, ADDTIME(ScheduledlArrivalTime, "00:10:00") 
AS ExpectedArrivalTime FROM ScheduleDetails;

Output :

TrainIdStationNameTrainNameScheduledlArrivalTimeExpectedArrivalTime
12345NJPSaraighat Express17:04:2217:14:22
My Personal Notes arrow_drop_up
Recommended Articles
Page :