With the DATEADD function we can add or remove a datepart to a date. Below is a list of the dateparts we can add.
The DATEADD Function is one of the most used date functions.
datepart | Abbreviations |
year | yy, yyyy |
quarter | qq, q |
month | mm, m |
dayofyear | dy, y |
day | dd, d |
week | wk, ww |
weekday | dw, w |
hour | hh |
minute | mi, n |
second | ss, s |
millisecond | ms |
microsecond | mcs |
nanosecond | ns |
Let’s make examples for a better understanding of DATEADD Function.
Add year to a date using DATEADD Function:
1 2 3 |
SELECT DATEADD(year,1,'2019-01-01') AS YEAR_DATEPART, DATEADD(yy,1,'2019-01-01') AS YY_DATEPART, DATEADD(yyyy,1,'2019-01-01') AS YYYY_DATEPART |
Add week to a date using DATEADD Function:
1 |
SELECT DATEADD(wk,1,'2019-01-01') AS week_wk_DATEPART |
You can use DATEADD function as follows:
1 2 3 |
DECLARE @dateaddexample datetime SET @dateaddexample='2019-01-01' SELECT DATEADD(wk,1,@dateaddexample) AS week_wk_DATEPART |
Remove week from a date using DATEADD Function:
1 |
SELECT DATEADD(wk,-1,'2019-01-01') AS week_wk_DATEPART |