Posted by: Neel Aakash on: December 23, 2011
DECLARE @startdate SMALLDATETIME = '2011-01-01' DECLARE @stopdate SMALLDATETIME = '2011-12-31' -- Create a Table DECLARE @weekTable TABLE ( Id INT IDENTITY (1, 1), StartDate SMALLDATETIME, EndDate SMALLDATETIME ) -- Find Monday at that week DECLARE @currentDate SMALLDATETIME = DATEADD(d, 2 - DATEPART(dw, @startdate), @startdate) WHILE @currentDate <= @stopdate BEGIN INSERT INTO @weekTable VALUES (@currentDate, DATEADD(d, 6, @currentDate)) SET @currentDate = DATEADD(ww, 1, @currentDate) END -- Return the results SELECT Id 'Week #', StartDate 'Start Date', EndDate 'End Date' FROM @weekTable