SQL > SQL Date Functions > Dateadd Function

The DATEADD function is used to add an interval to a date. This function is available in SQL Server.

Syntax

The syntax for the DATEADD function is as follows:

DATEADD (datepart, number, expression)

where the data type of <expression> is some type of date, time, or datetime. <number> is an integer (can be positive or negative). <datepart> can be one of the following:

datepartabbreviation
yearyy, yyyy
quarterqq, q
monthmm, m
dayofyeardy, y
daydd, d
weekwk, ww
hourhh
minutemi, n
secondss, s
millisecondms
microsecondmcs
nanosecondns
TZoffsettz
ISO_WEEKisowk, isoww

The result returned has the same data type as <expression>.

Examples

Below we show several examples of the output from applying the DATEADD function.

Example 1

The SQL statement,

SELECT DATEADD (day, 10, '2000-01-05 00:05:00.000');

produces the following result:

'2000-01-15 00:05:00.000'

In this example, we are adding 10 days to the original datetime.

Example 2

The SQL statement,

SELECT DATEADD (hour, -10, '2000-01-05 00:05:00.000');

produces the following result:

'2000-01-14 14:05:00.000'

In this example, we are subtracting 10 hours from the original datetime.

Example 3

The SQL statement,

SELECT DATEADD (mm, 1, '2000-01-05 00:05:00.000');

produces the following result:

'2000-02-15 00:05:00.000'

In this example, we use the abbreviation for "month" to add one month to the original datetime.

List of SQL Date Functions

Function NameDescription
DATEADDAdds an interval to a date value in SQL Server.
DATEDIFFCalculates the difference between two dates in MySQL and SQL Server.
DATEPARTExtracts a specific part of a date/time value in SQL Server.
GETDATERetrieves database time in SQL Server.
SYSDATERetrieves database time in Oracle and MySQL.

Next: SQL DATEDIFF




Copyright © 2017   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact