Pages

6/16/2011

DATEADD

Syntax

DATEADD ( datepart , number, date )

Arguments

datepart

Is the parameter that specifies on which part of the date to return a new value. The table lists the dateparts and abbreviations recognized by Microsoft® SQL Server™.

Datepart Abbreviations
Year yy, yyyy
quarter qq, q
Month mm, m
dayofyear dy, y
Day dd, d
Week wk, ww
Hour hh
minute mi, n
second ss, s
millisecond ms

number

Is the value used to increment datepart. If you specify a value that is not an integer, the fractional part of the value is discarded. For example, if you specify day for datepart and1.75 for number, date is incremented by 1.

date

Is an expression that returns a datetime or smalldatetime value, or a character string in a date format. For more information about specifying dates, see datetime and smalldatetime.

If you specify only the last two digits of the year, values less than or equal to the last two digits of the value of the two digit year cutoff configuration option are in the same century as the cutoff year. Values greater than the last two digits of the value of this option are in the century that precedes the cutoff year. For example, if two digit year cutoff is 2049 (default), 49 is interpreted as 2049 and 2050 is interpreted as 1950. To avoid ambiguity, use four-digit years.

Return Types

Returns datetime, but smalldatetime if the date argument is smalldatetime.

Examples

This example prints a listing of a time frame for titles in the pubs database. This time frame represents the existing publication date plus 21 days.


SELECT DATEADD(n, 1, EffDate) AS EfficeDate
FROM PriceList
WHERE ProductID='201102'
ORDER BY EffDate

Here is the result set:


EfficeDate
2010-03-30 13:31:00.000
2010-03-31 13:31:00.000
2010-04-01 13:31:00.000
2010-04-01 13:31:00.000
2010-04-03 05:31:00.000
2010-04-03 05:31:00.000
2010-04-05 13:31:00.000
2010-04-06 05:31:00.000

No comments: