25 May 2011

Summarizing by month and year with datepart

Database, MSSQL No Comments

There are many ways of searching and doing date calculations.

SELECT DATEPART(yyyy,sales_date) AS 'Year',
MAX(DATENAME(m,sales_date)) AS 'Month',
COUNT(sales_date) AS 'Units per Month'
FROM sales
GROUP BY DATEPART(yyyy,sales_date),
DATEPART(mm,sales_date)
ORDER BY DATEPART(yyyy,sales_date),
DATEPART(mm,sales_date);

No Responses to “Summarizing by month and year with datepart”

Leave a Reply