I have a table :
------------------------------------ ReportDate | DAGName | MailboxCount ------------------------------------ There is lot of records in this table . I have to get the sum of mailboxcount of each dagName on the 1st day of every month for a particular year.
------------------------------------ ReportDate | DAGName | MailboxCount ------------------------------------ 01-01-2012 | abc | 25 01-02-2012 | xyz | 55 01-02-2012 | abc | 125 01-03-2012 | lmn | 225 01-01-2012 | ghf | 325 01-03-2012 | kil | 525 11-03-2012 | kil | 525 21-03-2012 | kil | 625 10-05-2012 | jil | 225 20-11-2012 | kil | 1525 04-03-2012 | Mil | 5025 So what i want as the result is
--------------------------------- Month Name | Count --------------------------------- January | 350 Ferbuary | 150 March | 850 My Query
SELECT SUM(MailboxCount) AS Count,DagName ,MONTH(CAST(ReportDate AS DATETIME)) ,DATENAME(month, ReportDate) AS 'Month Name' FROM MailboxDatabase WHERE (CONVERT(VARCHAR, CONVERT(VARCHAR(10), [ReportDate], 103), 103) IN ( '01/01/'+ @year,'01/02/'+ @year, '01/03/'+ @year,'01/04/'+ @year, '01/05/'+ @year,'01/06/'+ @year, '01/07/'+ @year,'01/08/'+ @year, '01/09/'+ @year,'01/10/'+ @year, '01/11/'+ @year,'01/12/'+ @year )) GROUP BY MONTH(CAST(ReportDate AS DATETIME)),DATENAME(month, ReportDate),DagName ORDER BY 2 I am fine if some extra columns like coming with my query. But it is not giving me the correct result. Any help ??
VARCHARwithout specifying a length, or it will come back and haunt you.