How can I group by date time column without taking time into consideration
I have a bunch of product orders and I'm trying to group by the date and sum the quantity for that date. How can I group by the month/day/year without taking the time part into consideration?
3/8/2010 7:42:00
should be grouped with 3/8/2010 4:15:00
Solution 1:
Cast/Convert the values to a Date
type for your group by.
GROUP BY CAST(myDateTime AS DATE)
Solution 2:
GROUP BY DATEADD(day, DATEDIFF(day, 0, MyDateTimeColumn), 0)
Or in SQL Server 2008 onwards you could simply cast to Date
as @Oded suggested:
GROUP BY CAST(orderDate AS DATE)
Solution 3:
In pre Sql 2008 By taking out the date part:
GROUP BY CONVERT(CHAR(8),DateTimeColumn,10)
Solution 4:
GROUP BY DATE(date_time_column)
Solution 5:
CAST datetime field to date
select CAST(datetime_field as DATE), count(*) as count from table group by CAST(datetime_field as DATE);