Zied.M
Zied.M

Reputation: 215

Get month name from SQL Server date value in table column

I want to create a statistics query (display the number of records per month).

The problem is that I want the month displayed in letter such as January, February, March, etc. not like 1, 2 or 3. I is not displaying month name in alphabet using month(getdate().

I don't know how can I do the conversion. Here is my SQL Server query:

select count(*) [Total], month(date) [Mois] 
from user 
where year(date) >= 2018 
group by month(date) 
order by month(date) desc 

Upvotes: 2

Views: 6232

Answers (3)

Gordon Linoff
Gordon Linoff

Reputation: 1269793

I imagine that the query that you want is:

select year(date), datename(month, date), count(*)  as total
from user 
where year(date) >= 2018 
group by year(date), datename(month, date)
order by min(date);

Notes:

  • You should be including the year in your query. Your where suggests that the query could return more than one year.
  • Don't include year if you really want to combine months from multiple years, but I think that is unlikely.
  • Generally, you want the results in chronological order. A convenient method is to order by a date value in each group.

Upvotes: 1

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30565

you can use

SELECT FORMAT ([Date], 'MMMM') as date

Upvotes: 2

Suraj Kumar
Suraj Kumar

Reputation: 5643

You should try something like this

select convert(char(3), [date], 0)
select datename(month, [date])

Upvotes: 3

Related Questions