ADDDATE() function in MySQL
Last Updated : 13 Sep, 2024
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 in MySQL by understanding various examples.
ADDDATE() Function in MySQL
- The
ADDDATE()
function in MySQL adds a specified time interval to a date or datetime value. - This function can be used in two different formats:
Syntax:
ADDDATE(date, INTERVAL expr unit)
Parameter:
date
: The starting date or datetime value. expr
: The number of time units to add. unit
: The unit of time (e.g., DAY, MONTH, YEAR)adds
Returns:
It returns the date or DateTime after adding the interval.
Examples of ADDDATE() Function in MySQL
Example 1:
Let’s Adding 15 days with the specified date using ADDDATE Function.
SELECT ADDDATE('2020-08-20', INTERVAL 15 DAY)
as Updated_date;
Output:
Example 2:
Let’s Adding 30 minutes with the specified datetime using ADDDATE Function.
SELECT ADDDATE('2020-08-28 20:59:59', INTERVAL 30 MINUTE)
as Updated_datetime;
Output:
Updated_datetime |
2020-08-28 21:29:59 |
Example 3:
Let’s Adding 4 weeks with the specified date using ADDDATE Function.
SELECT ADDDATE('2020-08-12', INTERVAL 4 WEEK)
as Updated_date;
Output:
Example 4:
Let’s Adding 6 months with the specified date using ADDDATE Function.
SELECT ADDDATE('2019-08-12', INTERVAL 6 MONTH)
as Updated_date ;
Output:
Example 5:
Let’s Adding 10 years with the specified date using ADDDATE Function.
SELECT ADDDATE('2010-12-10', INTERVAL 10 YEAR)
as Updated_date ;
Output:
Example 6:
Let’s Adding 5 days 10 hour 05 minute 20 seconds with the specified datetime using ADDDATE Function.
SELECT ADDDATE('2020-08-20 05:15:19', INTERVAL '5-10-05-20' DAY_SECOND)
as Updated_datetime;
Output:
Updated_datetime |
2020-08-25 15:20:39 |
Example 7:
The ADDDATE 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 DATETIME NOT NULL,
PRIMARY KEY(TrainId )
);
Now inserting values in ScheduleDetails table. We will use ADDDATE function which will denote delay in arrival timing. The value in ExpectedArrivalTime column will be the value given by ADDDATE Function.
INSERT INTO
ScheduleDetails (TrainId, StationName, TrainName, ScheduledlArrivalTime )
VALUES
(12859, 'KGP', 'Gitanjali Express ', '2020-11-17 10:20:10');
Now, checking the ScheduleDetails table:
SELECT *, ADDDATE(ScheduledlArrivalTime, INTERVAL '0-5-05-20' DAY_SECOND)
AS ExpectedArrivalTime FROM ScheduleDetails;
Output:
TRAINID | STATIONNAME | TRAINNAME | SCHEDULEDARRIVALTIME | EXPECTEDARRIVALTIME |
12859 | KGP | Gitanjali Express | 2020-11-17 10:20:10 | 2020-11-17 15:25:30 |
Explanation: This query selects all columns from the `ScheduleDetails` table and uses the `ADDDATE()` function to add a time interval (`0 days, 5 hours, 5 minutes, 20 seconds`) to the `ScheduledlArrivalTime` column. The result is displayed in a new column named `ExpectedArrivalTime`, which shows the adjusted arrival time of the train.
Conclusion
The ADDDATE()
function in MySQL provides a versatile solution for handling date and time manipulations. With support for various time units and precise control over date adjustments, it makes tasks like scheduling and tracking time changes much easier. Whether you’re working with simple day additions or complex interval calculations, ADDDATE()
is an essential tool for database developers and users.
Similar Reads
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
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
DAY() Function in MySQL
DAY() function : This function in MySQL is used to return the day of the month for a specified date (a number from 1 to 31). This function equals the DAYOFMONTH() function. Syntax : DAY(date) Parameter : This method accepts a parameter which is illustrated below : date : Specified date to extract th
1 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
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
MySQL ADDTIME() function
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. SyntaxThe MySQL ADDTIME() function syntax is: ADDTIME(expr1, expr2) ParameterADDTIME() function accepts two parameters. expr1: The given dateti
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
BIT_AND() function in MySQL
BIT_AND() : This function in MySQL is used to return the Bitwise AND of all bits in a given expression. It first converts all decimal values into binary values, and then perform bitwise and operation on those binary values. The BIT_AND() function works by performing a bitwise AND operation on each p
3 min read
COT() Function in MySQL
COT() function : This function in MySQL is used to return the cotangent of a specified number. If the specified number is 0, an error or NULL will be returned. In a right triangle, the cotangent of an angle is the length of it's adjacent side divided by the length of the opposite side. Similarly, th
1 min read
DIV() Function in MySQL
DIV() function : This function in MySQL is used to return a quotient (integer) value when integer division is done. For example, when 7 is divided by 3, then 2 will be returned. Syntax : SELECT x DIV y; Parameter : This method accepts two parameters as given below as follows. x - Specified dividend
1 min read