The DateAdd() Functions adds/subtracts a specific interval to a specified date
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Dateadd(datepart,interval,date) --datepart: --year --quarter --month --dayofyear --day --week --weekday --hour --minute --second --millisecond --microsecond --nanosecond |
Example:
Using the DateAdd function in a loop to add 1 month to each date
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Declare @Counter int Set @Counter = 0 --Add for 24 months While @Counter < 24 Begin --increment counter set @Counter+= 1 --increment by month for given counter against todays date select DateAdd(MONTH, @Counter, GetDate()) End |