Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQL Server DATEADD() function

SQL Server DATEADD() function


SQL Dates SQL Server Date Functions


Definition and Usage

The DATEADD() function adds or subtracts a specified time interval from a date.

Syntax

DATEADD(datepart,number,date)

The date parameter is a valid date expression. number is the number of intervals you wish to add; for future times, this number is positive, for past times, this number is negative.

The datepart parameter can be one of the following values:

datepart Shorthands
Year yy, yyyy
Quarter qq, q
Month mm, m
Day of year dy, y
Day dd, d
Week wk, ww
Week dw, w
Hour hh
Minute mi, n
Second ss, s
Millisecond ms
Microsecond mcs
Nanosecond ns

Example

Suppose we have the following "Orders" table:

OrderId ProductName OrderDate
1 Jarlsberg Cheese 2008-11-11 13:23:44.657

Now, we want to add 45 days to "OrderDate" so that we can find the payment date.

We use the following SELECT statement:

SELECT OrderId,DATEADD(day,45,OrderDate) AS OrderPayDate

FROM Orders

Result:

OrderId OrderPayDate
1 2008-12-26 13:23:44.657

SQL Dates SQL Server Date Functions