Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Talk:How to find the first and last days in years, months etc
From Wiki
Something is wrong with your week calculation. I ran it for 20080601 and got 20080602 as the first day of the week.
I fixed it, Should have used
DATEADD(dw, DATEDIFF(dw, 0, @d), 0) AS FirstDayOfWeek, DATEADD(wk, DATEDIFF(wk, 0, @d)+1, -1) AS LastDayOfWeek
instead of
DATEADD(wk, DATEDIFF(wk, 0, @d), 0) AS FirstDayOfWeek, DATEADD(wk, DATEDIFF(wk, 0, @d)+1, -1) AS LastDayOfWeek
There was still something wrong, I used the following so it will work, until you come up with something more elegant if you want.
@d - DATEDIFF(dd, @@DATEFIRST - 1, @d) % 7 AS FirstDayOfWeek, @d - DATEDIFF(dd, @@DATEFIRST - 1, @d) % 7 + 6 AS LastDayOfWeek



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.