Get month and year from a datetime in SQL Server 2005

后端 未结 20 1585
[愿得一人]
[愿得一人] 2020-12-12 15:39

I need the month+year from the datetime in SQL Server like \'Jan 2008\'. I\'m grouping the query by month, year. I\'ve searched and found functions like datepart, convert, e

20条回答
  •  悲哀的现实
    2020-12-12 16:28

    Funny, I was just playing around writing this same query out in SQL Server and then LINQ.

    SELECT 
        DATENAME(mm, article.Created) AS Month, 
        DATENAME(yyyy, article.Created) AS Year, 
        COUNT(*) AS Total 
    FROM Articles AS article 
    GROUP BY 
        DATENAME(mm, article.Created), 
        DATENAME(yyyy, article.Created) 
    ORDER BY Month, Year DESC
    

    It produces the following ouput (example).

    Month | Year | Total
    
    January | 2009 | 2
    

提交回复
热议问题