DATEADD() Function in SQL Server


The DATEADD() function in SQL Server is used to add or subtract a specified time interval from a given date. The syntax of the DATEADD() function is as follows:

DATEADD(interval, number, date)

where interval is the time interval to be added or subtracted, number is the number of intervals to be added or subtracted, and date is the date to which the interval is to be added or subtracted.

The interval parameter can be one of the following values:

  • year
  • quarter
  • month
  • dayofyear
  • day
  • week
  • hour
  • minute
  • second
  • millisecond
  • microsecond
  • nanosecond

Here are some examples of using the DATEADD() function in SQL Server:

  • Adding 1 year to a date:
SELECT DATEADD(year, 1, '2021-01-01')
-- Output: 2022-01-01
  • Subtracting 3 months from a date:
SELECT DATEADD(month, -3, '2021-01-01')
-- Output: 2020-10-01
  • Adding 2 weeks to a date:
SELECT DATEADD(week, 2, '2021-01-01')
-- Output: 2021-01-15
  • Adding 5 hours to a date:
SELECT DATEADD(hour, 5, '2021-01-01 10:00:00')
-- Output: 2021-01-01 15:00:00
  • Adding 500 milliseconds to a date:
SELECT DATEADD(millisecond, 500, '2021-01-01 10:00:00.000')
-- Output: 2021-01-01 10:00:00.500

Note that the DATEADD() function can also be used in conjunction with other SQL Server functions, such as GETDATE() or CONVERT(), to perform more complex date calculations.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.