DATEADD function in SQL
The DATEADD function in SQL is used to add (or subtract) a specified time interval to a date and return the resulting date. It is commonly used for operations like adding days, months, or years to a date.
SQL Syntax
DATEADD(datepart, number, date)
The following are the parameters of the function:
- datepart: The part of the date you want to add to, such as year, month, day, etc.
- number: The number of intervals you want to add (or subtract if it's a negative value).
- date: The starting date from which the interval will be added or subtracted.
Example: Add 10 Days to the Current Date.
SQL Syntax
SELECT DATEADD(day, 10, GETDATE()) AS NewDate;
- datepart: day
- number: 10 (adds 10 days)
- GETDATE(): current date and time
If today is 2024-10-10, the result will be:
NewDate |
---|
2024-10-30 20:24:13.120 |