Greycastle Logo

Want to group by date? Cast to date!

When extracting timed statistics from SQL you usually want these stats grouped by some kind of time and more often than not this time unit is days. I used to do this unnecessarily complicated by converting the dates to varchars in formats I didn’t even want and maybe even use datepart(day, datetime) if the data spanned within a month. Then the good man Sören Helenelund told me about casting to date. Neat and gives me exactly what I want:

SELECT cast(getdate() as date)

It will output the today’s date in my localized format 2013-05-30 and can be used to group things or whatever. Wonderful and easy to remember. Thanks Sören!

© 2024 Greycastle