301

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

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
The Muffin Man
  • 19,585
  • 30
  • 119
  • 191

9 Answers9

501

Cast/Convert the values to a Date type for your group by.

GROUP BY CAST(myDateTime AS DATE)
AlG
  • 14,697
  • 4
  • 41
  • 54
Oded
  • 489,969
  • 99
  • 883
  • 1,009
32
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)
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
17

In pre Sql 2008 By taking out the date part:

GROUP BY CONVERT(CHAR(8),DateTimeColumn,10)
Kamyar
  • 18,639
  • 9
  • 97
  • 171
  • 2
    For explanation about the convert function and date time type (meaning of char(8) and 10), see http://www.w3schools.com/sql/func_convert.asp – super1ha1 Dec 16 '16 at 03:18
11

CAST datetime field to date

select  CAST(datetime_field as DATE), count(*) as count from table group by CAST(datetime_field as DATE);
Ruchira Nawarathna
  • 1,137
  • 17
  • 30
  • Repeats the [accepted answer](https://stackoverflow.com/a/6054176/861716). Please don't re-post answers. Instead, vote for answers that helped you. – Gert Arnold Jul 19 '22 at 17:15
  • @GertArnold it's not the same -- the accepted answer only recommends adding CAST to the GROUP BY clause, while this answer recommends adding it to the SELECT as well – M.M Aug 31 '22 at 04:48
8

GROUP BY DATE(date_time_column)

Aravindh Gopi
  • 2,083
  • 28
  • 36
2

Here's an example that I used when I needed to count the number of records for a particular date without the time portion:

select count(convert(CHAR(10), dtcreatedate, 103) ),convert(char(10), dtcreatedate, 103)
FROM dbo.tbltobecounted
GROUP BY CONVERT(CHAR(10),dtcreatedate,103)
ORDER BY CONVERT(CHAR(10),dtcreatedate,103)
Robert
  • 5,278
  • 43
  • 65
  • 115
  • 2
    The ORDER BY will not work as supposed because it will not treat it as date so it will sort by day – Ed_ Mar 02 '16 at 16:25
2

Here is the example works fine in oracle

select to_char(columnname, 'DD/MON/yyyy'), count(*) from table_name group by to_char(createddate, 'DD/MON/yyyy');
Radhakrishnan
  • 564
  • 4
  • 6
1

Well, for me it was pretty much straight, I used cast with groupby:

Example:

Select cast(created_at as date), count(1) from dbname.tablename GROUP BY cast(created_at as date)

Note: I am using this on MSSQL 2016.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
  • 1
    Repeats the [accepted answer](https://stackoverflow.com/a/6054176/861716). Please don't re-post answers. Instead, vote for answers that helped you. – Gert Arnold Jul 19 '22 at 17:15
-1

I believe you need to group by , in that day of the month of the year . so why not using TRUNK_DATE functions . The way it works is described below :

Group By DATE_TRUNC('day' , 'occurred_at_time')
  • 1
    date_trunc is for PostgreSQL not SQL Server which going by the tags, the OP was seeking a solution for SQL server. – Dave Hogan Jun 01 '20 at 15:44