How to Add Days to a Date in SQL
Use the DATEADD() function to quickly add days to a date in SQL.
This function uses three parameter values:
DATEADD(interval, number, date)
interval: The date (or other time interval) you want to add.
You can use any of these date/time parameters in your query:
- year, yyyy, yy = Year
- quarter, qq, q = Quarter
- month, mm, m = month
- dayofyear, dy, y = Day of the year
- day, dd, d = Day
- week, ww, wk = Week
- weekday, dw, w = Weekday
- hour, hh = hour
- minute, mi, n = Minute
- second, ss, s = Second
- millisecond, ms = Millisecond
number: The numeric quantity of interval you want to add to the date.
This value can be positive if you want dates in the future, or negative if you want to retrieve past dates.
date: The date to be modified.
Formatted as YYYY/MM/DD
For example, if you wanted to add 7 days to the date 2020-01-01, you would use the following SQL query:
SELECT DATEADD(day, 7, '2020-01-01');
DATEADD() Examples
Let's look at a few examples of how this function can be used. Suppose you have a table called orders with the following data:
2020-01-01 |
2020-01-15 |
2020-02-01 |
If you wanted to add 7 days to each of these dates, you would use the following query:
SELECT DATEADD(day, 7, order_date) FROM orders;
This query would return the results 2020-01-08, 2020-01-22, and 2020-02-08, respectively.
Additional Info About SQL's DATEADD() function
The DATEADD() function is supported by most major databases, including MySQL, PostgreSQL, and SQL Server.
However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database.