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
Datepart | Abbreviations | Description |
---|---|---|
year | yy,yyyy | This will add specified number of years (in second argument) to the given date |
quarter | qq, q | This will add specified number of Quarters (in second argument) to the given date |
month | mm, m | This will add specified number of Months (in second argument) to the given date |
dayofyear | dy, y | This will add specified number of dayofyear (in second argument) to the given date |
day | dd, d | This will add specified number of days (in second argument) to the given date |
week | wk, ww | This will add specified number of Weeks (in second argument) to the given date |
weekday | dw, w | This will add specified number of Weekdays (in second argument) to the given date |
hour | hh | This will add specified number of Hours (in second argument) to the given date |
minute | mi, n | This will add specified number of Minutes (in second argument) to the given date |
second | ss, s | This will add specified number of Seconds (in second argument) to the given date |
millisecond | ms | This will add specified number of Milliseconds (in second argument) to the given date |
microsecond | mcs | This will add specified number of Microseconds (in second argument) to the given date |
nanosecond | ns | This 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
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
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
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