I was messing with some dates, and found a quick little bit that changed a date to the 1st day of the month:declare @d date = '11/23/2012'SELECT DATEADD(mm, 0 + DATEDIFF(mm, 0, @d), 0)
Naturally, someone now needed a date converted to the 1st day of a quarter. This little algorithm assumes that quarters start Jan 1st.SELECT DATEADD(mm, - CASE WHEN (MONTH(DATEADD(mm, 0 + DATEDIFF(mm, 0, @d), 0) )% 3) > 0 THEN ((MONTH(DATEADD(mm, 0 + DATEDIFF(mm, 0, @d), 0) )% 3) -1) ELSE 2 END ,DATEADD(mm, 0 + DATEDIFF(mm, 0, @d), 0) )
Any suggestions or comments would be appreciated.How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx