How to group by month from Date field using sql

How can I group only by month from the date field (and not by day)?

Here's what my date field looks like:

2012-05-01 

Here is my current SQL:

 select Closing_Date, Category, COUNT(Status)TotalCount from MyTable where Closing_Date >= '2012-02-01' and Closing_Date <= '2012-12-31' and Defect_Status1 is not null group by Closing_Date, Category 
+45
sql sql-server tsql
Jan 28 '13 at 15:50
source share
6 answers

I would use this:

 SELECT Closing_Date = DATEADD(MONTH, DATEDIFF(MONTH, 0, Closing_Date), 0), Category, COUNT(Status) TotalCount FROM MyTable WHERE Closing_Date >= '2012-02-01' AND Closing_Date <= '2012-12-31' AND Defect_Status1 IS NOT NULL GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, Closing_Date), 0), Category; 

This will be grouped by the first of each month, therefore

 `DATEADD(MONTH, DATEDIFF(MONTH, 0, '20130128'), 0)` 

will give '20130101' . I usually prefer this method as it saves dates as dates.

Alternatively, you can use something like this:

 SELECT Closing_Year = DATEPART(YEAR, Closing_Date), Closing_Month = DATEPART(MONTH, Closing_Date), Category, COUNT(Status) TotalCount FROM MyTable WHERE Closing_Date >= '2012-02-01' AND Closing_Date <= '2012-12-31' AND Defect_Status1 IS NOT NULL GROUP BY DATEPART(YEAR, Closing_Date), DATEPART(MONTH, Closing_Date), Category; 

It really depends on your desired result. (The closing year is not needed in your example, but if the date range crosses the year, it could be).

+54
Jan 28 '13 at 16:01
source share

Use the DATEPART function to retrieve a month from the date.

So you would do something like this:

 SELECT DATEPART(month, Closing_Date) AS Closing_Month, COUNT(Status) AS TotalCount FROM t GROUP BY DATEPART(month, Closing_Date) 
+27
Jan 28 '13 at 16:00
source share

I tried the solution user1845584. sql gave an error in the Category field. So I deleted it and it worked. I also added:

 select Year(Closing_Date), Month(Closing_Date)" 

It was better. This means that I transferred the bill according to the year and month.

+3
Oct 21 '15 at 7:02
source share

I used FORMAT to do the following:

 select FORMAT(Closing_Date, 'yyyy_MM') AS Closing_Month , count(*) cc FROM MyTable WHERE Defect_Status1 IS NOT NULL AND Closing_Date >= '2011-12-01' AND Closing_Date < '2016-07-01' GROUP BY FORMAT(Closing_Date, 'yyyy_MM') ORDER BY Closing_Month 
+2
Oct 05 '16 at 1:55
source share

By adding MONTH(date_column) to GROUP BY .

 SELECT Closing_Date, Category, COUNT(Status)TotalCount FROM MyTable WHERE Closing_Date >= '2012-02-01' AND Closing_Date <= '2012-12-31' AND Defect_Status1 IS NOT NULL GROUP BY MONTH(Closing_Date), Category 
0
04 Oct '17 at 10:25
source share

You can do this using Year (), Month () Day () and datepart ().

In your example, this would be:

 select Closing_Date, Category, COUNT(Status)TotalCount from MyTable where Closing_Date >= '2012-02-01' and Closing_Date <= '2012-12-31' and Defect_Status1 is not null group by Year(Closing_Date), Month(Closing_Date), Category 
-one
Jan 28 '13 at 16:00
source share



All Articles