Wednesday, August 14, 2013

SQL DATEADD


SQL DATEADD


SQL DATEADD function is used to add specific date interval to the existing date. For example, For every product order we will save the Ordered Date and we can use this SQL Dateadd function to generate the expected delivery date.

SQL DATEADD Syntax

Datepart: It is the part of a given data to which we are going add the Number argument. Following table will display the list of available datepart argument in SQL Server 2014
DatepartAbbreviationsDescription
yearyy,yyyyThis will add specified number of years (in second argument) to the given date
quarterqq, qThis will add specified number of Quarters (in second argument) to the given date
monthmm, mThis will add specified number of Months (in second argument) to the given date
dayofyeardy, yThis will add specified number of dayofyear (in second argument) to the given date
daydd, dThis will add specified number of days (in second argument) to the given date
weekwk, wwThis will add specified number of Weeks (in second argument) to the given date
weekdaydw, wThis will add specified number of Weekdays (in second argument) to the given date
hourhhThis will add specified number of Hours (in second argument) to the given date
minutemi, nThis will add specified number of Minutes (in second argument) to the given date
secondss, sThis will add specified number of Seconds (in second argument) to the given date
millisecondmsThis will add specified number of Milliseconds (in second argument) to the given date
microsecondmcsThis will add specified number of Microseconds (in second argument) to the given date
nanosecondnsThis will add specified number of Nanoseconds (in second argument) to the given date
Date: Please specify the valid date as third argument and it can be column, expression or any variable.
Number: Please specify the number you want to add.
  • If you specify the positive integer as the second argument (Number), SQL DATEADD function will add the specified number to the given date.
  • If you specify the Negative integer as the second argument (Number), SQL DATEADD function will subtract the specified number from the given date.
  • If you specify the Decimal number as the second argument (Number), SQL DATEADD function will truncate the decimal values and add specified number to the given date.

Positive integer in SQL DATEADD

In this example we are going to use the positive integer as a second argument
SQL CODE
OUTPUT
SQL Dateadd 1
ANALYSIS
We declared one variable and assigned the date and time to that variable
We asked the SQL Dateadd function to add 1 year to the given date
We asked the SQL Dateadd function to add 1 month to the given date

Negative Integer in SQL DATEADD

In this example we are going to use the Negative integer as a second argument
SQL CODE
OUTPUT
SQL Dateadd 2
ANALYSIS
We asked the SQL Dateadd function to add 1 year to the given date but the function is subtracting 1 year from it. This is because Negative value is used to subtract
We asked the SQL Dateadd function to add 1 Month to the given date but the function is subtracting 1 Month from it. This is because Negative value is used to subtract

Decimal Values in SQL DATEADD

In this example we are going to use the decimal value as a second argument
SQL CODE
OUTPUT
SQL Dateadd 3
ANALYSIS
We asked the SQL Dateadd function to add 2.23 years to the given date but the function added 2 years only. This is because .23 values are truncated
We asked the SQL Dateadd function to add 4.51 months to the given date but the function subtracted 4 months from given date. This is because, Negative value is used to subtract and .51 value is truncated
Thank You for Visiting Our Blog