Subscribe to our YouTube Channel for more Videos

SQL Server 101 – GROUP BY datetime and ignore time

Assume that you have a Order table and you want to query the table to retreive the total orders made each day. You can do this by using the GROUP BY with the Datetime fields being type casted to date as shown below.

How to Group By datetime and ignore time in SQL Server ?

SELECT CAST(OrderDateTime AS DATE) as OrderDate, COUNT(1) as TotalOrders
FROM Orders
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.