AngelEyes on .Net

Tips, tricks and WTFs about Asp .Net, SQL Server and the likes.
posts - 54 , comments - 235 , trackbacks - 6

My Links


Tag Cloud


3rd party

Official ASP .NET

Useful info

Thursday, June 17, 2010

How to: group by month with SQL

I took this particular code from,

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:



        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:


ORDER BY dateadd(month, datediff(month, 0, SomeDate),0)

Posted On Thursday, June 17, 2010 10:14 AM | Comments (0) |

Powered by: