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

When dealing with a relational database management system (RDBMS) like SQL Server, compatibility level is an important concept to understand....
In this blog post, let’s learn about the error message “49975 – Unable to load controller client certificate due to...
In this blog post, let’s learn about the error message “49973 – Cannot remove tempdb remote file to local tempdb...