Graham
Graham

Reputation: 1960

How to get month name out of date field

I'm trying to build a query that gives me a year month field with month names instead of the numbers. The output should be similar to this:

Year/month field  | Sum field
Januari 2014          100
Februari 2014       12300
Maart 2014           3234
April 2014           4964

I've searched for the way to get the the month name but I must be doing something wrong because I constantly get the error that datename is not a function or an sql error 1064. This i what i've tried:

SELECT id, 
       YEAR(report_date) AS YEAR, 
       MONTH(report_date) AS MONTH,
SELECT DATENAME(MONTH, (MONTH(report_date))) ,
CONVERT(VARCHAR(3), DATENAME(MONTH, report_date)) SUM(num) AS participants_month
FROM participants
WHERE unit_id = 10
GROUP BY unit_id,
  MONTH(report_date)
ORDER BY report_date ASC;

Tried to make this question as clear as possible. If something isn't clear tell me and I'll try to explain it. Really hope someone could point me out in the right direction.

Upvotes: 0

Views: 2132

Answers (2)

mareckmareck
mareckmareck

Reputation: 1580

In SQL Server you use DATENAME(month, date): SQL Server Fiddle

But in MySQL you use MONTHNAME(date): MySQL Fiddle

From the error you are receiving I guess you do not use SQL Server as your database management system, so in MySQL you should use MONTHNAME(date).

Upvotes: 2

Olesya Razuvayevskaya
Olesya Razuvayevskaya

Reputation: 1168

From the error, I suppose, you are using MySQL.But the DATENAME() is Transact-SQL function.

In MySQL, you can use

  MONTH(yourdate)

instead

Upvotes: 0

Related Questions