MySQL ADDTIME() function adds the specified time intervals to the given date and time. It returns the date or DateTime value after adding the time interval.
Syntax
The MySQL ADDTIME() function syntax is:
ADDTIME(expr1, expr2)
Parameter
ADDTIME() function accepts two parameters.
- expr1: The given datetime or time that we want to modify.
- expr2: The time interval which we want to add to a given datetime. It can be both positive and negative.
MySQL ADDTIME() Function Example
Let’s look at some examples of the ADDTIME() function in MySQL. Learning MySQL ADDTIME function with examples, help in understanding the concept better.
Example 1
In this example, we are adding 15 seconds to the specified time using the ADDTIME function.
SELECT ADDTIME("11:34:21", "15")
AS Updated_time ;
Output :
Example 2
In this example, we are adding 10 minutes to the specified time using the ADDTIME function.
SELECT ADDTIME("10:54:21", "00:10:00")
AS Updated_time ;
Output :
Example 3
In this example, we are 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 |
Using ADDTIME function on Table Column Values
In this example, we will use the ADDTIME function on the values of columns.
First, let’s 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 the ScheduleDetails table. We will use ADDTIME function, which will denote delay in arrival timing. The value in the ExpectedArrivalTime column will be the value given by the 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 :
TrainId |
StationName |
TrainName |
ScheduledlArrivalTime |
ExpectedArrivalTime |
12345 |
NJP |
Saraighat Express |
17:04:22 |
17:14:22 |
Important Points About MySQL ADDTIME() Function
- The ADDTIME() function adds a time interval to a time or datetime value and returns the resulting time or datetime value.
- The ADDTIME() function is available in various versions of MySQL, including MySQL 5.7, 5.6, 5.5, 5.1, 5.0, and 4.1.1
- You can subtract time intervals by using negative values.
- If either expr1 or expr2 evaluates to NULL, the function returns NULL.
- While it can work with time and datetime values, ADDTIME() primarily focuses on modifying datetime values.
Similar Reads
ADDDATE() function in MySQL
The ADDDATE() function in MySQL is a powerful tool for adding specific time intervals to date or datetime values. It simplifies data manipulation by allowing us to easily calculate future or adjusted dates based on a given starting point. In this article, we will learn about the ADDDATE() function i
3 min read
DATE_ADD() Function in MySQL
DATE_ADD() function in MySQL is used to add a specified time or date interval to a specified date and then return the date. Syntax: DATE_ADD(date, INTERVAL value addunit) Parameter: This function accepts two parameters which are illustrated below: date - Specified date to be modified. value addunit
2 min read
CURTIME() function in MySQL
CURTIME() function in MySQL is used to check the current time. It returns the current time as a value in âhh:mm:ssâ or 'hhmmss' format, depending on whether the function is used in a string or numeric context. Syntax : CURTIME(fsp) Parameters : This method accepts only one parameter. fsp - It specif
2 min read
MySQL DATE_SUB() Function
The MySQL DATE_SUB() function subtracts a specified time or date interval from a datetime value. DATE_SUB() Function in MySQLThe DATE_SUB() Function in MySQL allows for various date and time calculations by subtracting time intervals. It is used to subtract a specified time or date interval from a g
2 min read
MySQL Date and Time Functions
Handling date and time data in MySQL is essential for many database operations, especially when it comes to handling timestamps, scheduling tasks, or generating time-based. MySQL provides a variety of date and time functions that help users work with date values, perform calculations, and format the
6 min read
PLSQL | DBTIMEZONE Function
The PLSQL DBTIMEZONE function is used for returning the database time zone value. The PLSQL DBTIMEZONE function does not require any parameter to be passed. The DBTIMEZONE function returns a time zone offset and it follows a format of '[+|-]TZH:TZM for example -05:00. It may also return the time zon
1 min read
CURDATE() Function in MySQL
The CURDATE() function in MYSQL is used to return the current date. The date is returned to the format of "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). This function equals the CURRENT_DATE() function. In this article, we are going to discuss about CURDATE() function in detail. Syntax CURDATE(); P
2 min read
DAYNAME() Function in MySQL
DAYNAME() function : This function in MySQL is used to return the weekday name for a specified date. Syntax : DAYNAME(date) Parameter : This method accepts a parameter which is illustrated below as follows. date - Specified date to extract the weekday name from Returns : It returns the weekday name
1 min read
UTC_DATE() function in MySQL
UTC_DATE() function in MySQL is used to check current Coordinated Universal Time (UTC) date. It returns the UTC date value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in string or numeric context. Syntax : UTC_DATE OR UTC_DATE() Parameter : This method does not acce
2 min read
DATEDIFF() Function in MySQL
DATEDIFF() function in MySQL is used to return the number of days between two specified date values. Syntax: DATEDIFF(date1, date2) Parameter: This function accepts two parameters as given below: date1: First specified datedate2: Second specified date Returns : It returns the number of days between
2 min read