If you use Microsoft Outlook, you know that when you set up a meeting or appointment, you can select an option like â€œThe 4th Monday of every monthâ€?. Well, today I had to correlate this to a SQL statement and it through me for a loop: Well, yeah, I used a loop. Check it out
DECLARE @tempDate DATETIME
DECLARE @maxDate DATETIME
SET @maxDate = ’07/01/2006′
SET @tempDate = ’07/01/2005′
WHILE @tempDate <= @maxDate BEGIN
SET @tempDate = DATEADD(mm,1,@tempDate)
I am getting the first Monday of the month in @tempDate and adding 3 weeks to it. Then I just loop through from @tempdate to @maXDate. Works like a champ.