dateadd

Description

Adds an interval to a specified date or time. It takes three arguments: the datepart, a number, and a chronological expression. The datatype of the result is the same as the datatype of the last argument with a value equal to the original value 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.

Syntax

dateadd(date_part, integer, {date | time | bigtime | datetime, | bigdatetime})

Parameters

date_part

is a date part or abbreviation. For a list of the date parts and abbreviations recognized by Adaptive Server, see “Date parts”.

numeric

is an integer expression.

date expression

is an expression of type datetime, smalldatetime, bigdatetime, bigtime, date, time, or a character string in a datetime format.

Examples

Example 1

Adds one million microseconds to a bigtime:

declare @a bigtime
select @a = "14:20:00.010101"
select dateadd(us, 1000000, @a)
------------------------------
2:20:01.010101PM

Example 2

Adds 25 hours to a bigdatetime and the day will increment:

declare @a bigdatetime
select @a = "apr 12, 0001 14:20:00 "
select dateadd(hh, 25, @a)
------------------------------
Apr 13 0001   2:20PM

Example 3

Displays the new publication dates when the publication dates of all the books in the titles table slip by 21 days:

select newpubdate = dateadd(day, 21, pubdate) 
from titles 

Example 4

Adds one day to a date:

declare @a date
select @a = "apr 12, 9999"
select dateadd(dd, 1, @a)
--------------------------
Apr 13 9999

Example 5

Subtracts five minutes to a time:

select dateadd(mi, -5, convert(time, "14:20:00"))
--------------------------
2:15PM

Example 6

Adds one day to a time and the time remains the same:

declare @a time
select @a = "14:20:00"
select dateadd(dd, 1, @a)
--------------------------
2:20PM

Example 7

Adds higher values resulting in the values rolling over to the next significant field, even though there are limits for each date_part, as with datetime values:

--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 

Usage

Table 2-8: date_part recognized abbreviations

Date part

Abbreviation

Values

Year

yy

1753 – 9999 (datetime)

1900 – 2079 (smalldatetime)

0001 – 9999 (date)

Quarter

qq

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

Standards

ANSI SQL – Compliance level: Transact-SQL extension.

Permissions

Any user can execute dateadd.

See also

Datatypes Date and time datatypes

Commands select, where clause

Functions datediff, datename, datepart, getdate