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.
Getting the First and last days of month, quarter and week
From Wiki
By using dateadd and datediff together you can get the first and last day for years, quarters, months and weeks(be careful of ISO weeks!) Run the code below to see how this works
- DECLARE @d DATETIME
- SET @d = '20071010'
- SELECT DATEADD(yy, DATEDIFF(yy, 0, @d), 0) AS FirstDayOfYear,
- DATEADD(yy, DATEDIFF(yy, 0, @d)+1, -1) AS LastDayOfYear,
- DATEADD(qq, DATEDIFF(qq, 0, @d), 0) AS FirstDayOfQuarter,
- DATEADD(qq, DATEDIFF(qq, 0, @d)+1, -1) AS LastDayOfQuarter,
- DATEADD(mm, DATEDIFF(mm, 0, @d), 0) AS FirstDayOfMonth,
- DATEADD(mm, DATEDIFF(mm, 0, @d)+1, -1) AS LastDayOfMonth,
- DATEADD(wk, DATEDIFF(wk, 0, @d), 0) AS FirstDayOfWeek,
- DATEADD(wk, DATEDIFF(wk, 0, @d)+1, -1) AS LastDayOfWeek
Change the value of the @d variable to test with other dates
Contributed by: SQLDenis



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