DATEADD function in SQL Server

This article will show you in detail how to use the DATEADD () datetime processing function in SQL Server with specific syntax and examples to better visualize and capture functions.

Describe

The DATEADD function in SQL Server returns a new time value when it is added a specified time period.

Syntax

To use the DATEADD function in SQL Server, we use the following syntax:

 DATEADD(dangthoigian, number, thoigian) 

Parameters :

  1. dangthoigian: the type of time used to calculate addition to thoigian. It may be one of the following values:
    Value Explanation year, yyyy, yy Yearquarter, qq, qQuýmonth, mm, mThdaydayofyearDate in daysday, dy, yYdayweek, ww, wkTuwwdayday, dw, w Day of the day, hhGinminute, mi, nPhútsecond, ss, s Millisecond, msMis second
  2. number: the amount of time you want to add.
  3. thoigian: the amount of time you want to add numbers .

Note :

  1. If the number parameter > 0 , DATEADD function will understand that this is the number of times to be incremented and added to the thoigian parameter .
  2. If the number <0 parameter, DATEADD function will understand this is the number of times you want to reduce and subtract from the thoigian parameter .
  3. If the number parameter is in decimal , DATEADD will only use the integer part (remove the decimal part).
  4. DATEADD can be used in later versions of SQL Server: SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, and SQL Server 2005.

For example

Take a look and explore some examples of DATEADD functions in SQL Server.

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

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

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

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

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

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

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

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

Previous article: DATEDIFF function in SQL Server

Next article: DATENAME function in SQL Server

4 ★ | 1 Vote

May be interested

  • DATEPART function in SQL ServerPhoto of DATEPART function in SQL Server
    the datepart function in sql server returns a time value of the input argument, which can be day, month, year, quarter, hour, minute, second, millisecond ... the return value is an integer type (int)
  • IF commands ... ELSE in SQL ServerPhoto of IF commands ... ELSE in SQL Server
    like other programming languages, sql server also provides users with an if command el .... the article will detail how to use the syntax and clear examples to make it easier for you to imagine if ... else.
  • BREAK (Control Interrupt) command in SQL ServerPhoto of BREAK (Control Interrupt) command in SQL Server
    the break command used to exit the loop does not specify a stop condition or you want to stop the loop on condition that you specify and execute the statements following the loop statement end.
  • CONTINUE command in SQL ServerPhoto of CONTINUE command in SQL Server
    the continue command is used to give command execution authority to the last expression of the loop. that means upside-down to the top of the loop, all the commands that follow in the loop containing continue will be ignored without execution.
  • GOTO command in SQL ServerPhoto of GOTO command in SQL Server
    the goto command is a simple jump command, which allows an unconditional jump program from goto to a location in the program that has a label (laber) command in the same function.
  • Instructions for installing SQL Server 2019Photo of Instructions for installing SQL Server 2019
    on september 24, microsoft announced the release of sql server 2019 community technical preview (ctp) 2.0. it is very suitable for database professionals to keep up with modern technology.