SQL Server GROUP BY datetime ignore hour minute and a select with a date and sum value SQL Server GROUP BY datetime ignore hour minute and a select with a date and sum value sql-server sql-server

SQL Server GROUP BY datetime ignore hour minute and a select with a date and sum value


SELECT CAST(Datetimefield AS DATE) as DateField, SUM(intfield) as SumFieldFROM MyTableGROUP BY CAST(Datetimefield AS DATE)


As he didn't specify which version of SQL server he uses (date type isn't available in 2005), one could also use

SELECT CONVERT(VARCHAR(10),date_column,112),SUM(num_col) AS summedFROM table_nameGROUP BY CONVERT(VARCHAR(10),date_column,112)


I came researching the options that I would have to do this, however, I believe the method I use is the simplest:

SELECT COUNT(*),        DATEADD(dd, DATEDIFF(dd, 0, date_field),0) as dtgroup FROM TABLE GROUP BY DATEADD(dd, DATEDIFF(dd, 0, date_field),0) ORDER BY dtgroup ASC;