June 2010 Entries

How to: group by month with SQL

I took this particular code from http://weblogs.sqlteam.com/... a good read. Shows you what to avoid and why. The recommended technique is the following: GROUP BY dateadd(month, datediff(month, 0, SomeDate),0) By the way, in the "select" clause, you can use the following: SELECT month(dateadd(month, datediff(month, 0, SomeDate),0)) as [month], year(dateadd(month, datediff(month, 0, SomeDate),0)) as [year], Just remember to also sort properly if needed: ......