Adds an interval to a specified date or time.
dateadd(date_part, integer, {date | time | bigtime | datetime, | bigdatetime})
declare @a bigtime select @a = "14:20:00.010101" select dateadd(us, 1000000, @a)
------------------------------ 2:20:01.010101PM
declare @a bigdatetime select @a = "apr 12, 0001 14:20:00 " select dateadd(hh, 25, @a) ------------------------------ Apr 13 0001 2:20PM
select newpubdate = dateadd(day, 21, pubdate) from titles
declare @a date select @a = "apr 12, 9999" select dateadd(dd, 1, @a) -------------------------- Apr 13 9999
select dateadd(mi, -5, convert(time, "14:20:00")) -------------------------- 2:15PM
declare @a time select @a = "14:20:00" select dateadd(dd, 1, @a) -------------------------- 2:20PM
--Add 24 hours to a datetime select dateadd(hh, 24, "4/1/1979") -------------------------- Apr 2 1979 12:00AM --Add 24 hours to a date select dateadd(hh, 24, "4/1/1979") ------------------------- Apr 2 1979
dateadd, a date function, adds an interval to a specified date. For information about dates, see Transact-SQL Users Guide.
dateadd takes three arguments: the date part, a number, and a date. The result is a datetime value equal to the date plus the number of date parts. If the last argument is a bigtime, and the datepart is a year, month, or day, the result is the original bigtime argument.
If the date argument is a smalldatetime value, the result is also a smalldatetime. You can use dateadd to add seconds or milliseconds to a smalldatetime, but such an addition is meaningful only if the result date returned by dateadd changes by at least one minute.
If a string is given as an argument in place of the chronological value the server interprets it as a datetime value regardless of its apparent precision. This default behavior may be changed by setting the configuration parameter builtin date strings or the set option builtin_date_strings. When these options are set, the server interprets strings given to chronological builtins as bigdatetimes. See the System Administration Guide for more information.
When a datepart of microseconds is given to this built-in string, values are always interpreted as bigdatetime.
Use the datetime datatype only for dates after January 1, 1753. datetime values must be enclosed in single or double quotes. Use the date datatype for dates from January 1, 0001 to 9999. date must be enclosed in single or double quotes.Use char, nchar, varchar, or nvarchar for earlier dates. The SAP ASE server recognizes a wide variety of date formats.
The SAP ASE server automatically converts between character and datetime values when necessary (for example, when you compare a character value to a datetime value).
Using the date part weekday or dw with dateadd is not logical, and produces spurious results. Use day or dd instead.
Date part |
Abbreviation |
Values |
---|---|---|
Year |
yy |
1753 – 9999 (datetime) 1900 – 2079 (smalldatetime) 0001 – 9999 (date) |
Quarter |
1 – 4 |
|
Month |
mm |
1 – 12 |
Week |
wk |
1054 |
Day |
dd |
1 – 7 |
dayofyear |
dy |
1 – 366 |
Weekday |
dw |
1 – 7 |
Hour |
hh |
0 – 23 |
Minute |
mi |
0 – 59 |
Second |
ss |
0 – 59 |
millisecond |
ms |
0 – 999 |
microsecond |
us |
0 – 999999 |
System Administration Guide, Transact-SQL Users Guide
select, where clause in Reference Manual: Commands
ANSI SQL – Compliance level: Transact-SQL extension.
Any user can execute dateadd.