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
GROUP BY CAST(OrderDateTime AS DATE)

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this blog post, let’s learn about the error message “1459 – An error occurred while accessing the database mirroring...
In this blog post, let’s learn about the error message “7937 – Columnstore index has one or more missing column...