SQL Server DATEADD Function Explained

The DATEADD function in SQL Server is used to add a specified time interval to a date or time value.

The syntax for the DATEADD function is as follows:

DATEADD(datepart, number, date)

Explanation of the parameters:

  1. Specifies the time interval part to be added, which can be one of the following values:
    year, yy, yyyy: year
    quarter, qq, q: quarter
    month, mm, m: month
    day, dd, d: day
    week, wk, ww: week
    hour, hh: hour
    minute, mi, n: minute
    second, ss, s: second
    millisecond, ms: millisecond
  2. number: specifies the quantity of time interval to be added, which can be a positive, negative, or zero value.
  3. Date: Specifies the date or time value to which you want to add a time interval.

1. He is not only intelligent but also charismatic.

SELECT DATEADD(year, 1, '2020-01-01') AS NewDate;
-- 结果为 '2021-01-01'

SELECT DATEADD(month, -6, GETDATE()) AS NewDate;
-- 结果为当前日期减去6个月的日期值

By using the DATEADD function in SQL Server, you can easily add or subtract a specified time interval from a date or time value.

bannerAds