SQL Server: DATEADD Function

DATEADD function

This SQL Server tutorial explains how to use the DATEADD function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the DATEADD function returns a date after which a certain time/date interval has been added.

Syntax

The syntax for the DATEADD function in SQL Server (Transact-SQL) is:

DATEADD( interval, number, date )

Parameters or Arguments

interval

The time/date interval that you wish to add. It can be one of the following values:

Value (any one of)Explanation
year, yyyy, yyYear interval
quarter, qq, qQuarter interval
month, mm, mMonth interval
dayofyearDay of year interval
day, dy, yDay interval
week, ww, wkWeek interval
weekday, dw, wWeekday interval
hour, hhHour interval
minute, mi, nMinute interval
second, ss, sSecond interval
millisecond, msMillisecond interval

numberThe number of intervals that you wish to add.dateThe date to which the interval should be added.

Note

  • If you specify a positive value for the number parameter, the DATEADD function will add the interval to the date.
  • If you specify a negative value for the number parameter, the DATEADD function will subtract the interval from the date.
  • And If you specify a decimal value for the number parameter, the DATEADD function will only use the integer portion of the number (and discard the decimal portion).

Applies To

The DATEADD functions can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let’s look at some SQL Server DATEADD functions examples and explore how to use the DATEADD functions in SQL Server (Transact-SQL).

For example:

SELECT DATEADD(year, 1, '2014/04/28');
Result: '2015-04-28 00:00:00.000'

SELECT DATEADD(yyyy, 1, '2014/04/28');
Result: '2015-04-28 00:00:00.000'

SELECT DATEADD(yy, 1, '2014/04/28');
Result: '2015-04-28 00:00:00.000'

SELECT DATEADD(year, -1, '2014/04/28');
Result: '2013-04-28 00:00:00.000'

SELECT DATEADD(month, 1, '2014/04/28');
Result: '2014-05-28 00:00:00.000'

SELECT DATEADD(month, -1, '2014/04/28');
Result: '2014-03-28 00:00:00.000'

SELECT DATEADD(day, 1, '2014/04/28');
Result: '2014-04-29 00:00:00.000'

SELECT DATEADD(day, -1, '2014/04/28');
Result: '2014-04-27 00:00:00.000'

Leave a Reply