In SQL, DATEADD function is used to add or subtract specific 'date/time interval' to a date or time.
DATEADD(datepart, number, date )
• Specify the 'datepart' that indicates the specific portion of the date or time to which the numeric value should be added. i.e. second, minutes, hour, day, week, month, year, etc.
• Specify the 'number' of 'datepart intervals' that you want to to add or subtract.
• Specify the 'date' or 'datetime' value to which the interval is added or subtracted.
1. Let's assume we have a table named "[Employees]".
2. Let's assume you want to add 10 years to the Joining_Date column for each employee.
3. Run below SQL statement :
SELECT *, DATEADD(YEAR, 10, Joining_Date) AS Revised_Joining_Date FROM [Employees];
4. In above statement, DATEADD function adds 10 years to the Joining_Date column for each employee and returns result in a column named 'Revised_Joining_Date'.
Using this function in a select statement won't modify the [Employees] table directly, but it will only be reflected in the select statement's output.
The Dateadd function in SQL adds a specified time interval to a date.
To add 30 days to a date in SQL, use the DATEADD function with the appropriate parameters.
Date_add in SQL is similar to DATEADD; it adds a specified time interval to a date.
To add 7 days to a date in SQL, use the DATEADD function with the interval set to 'day' and the number of days as the value.