rickyProgrammer
rickyProgrammer

Reputation: 1167

SQL Server : query to return month name instead of number

My goal is to return a row of month name with a column of Year. This is to get the sum of sales of one particular tenant in a per year and month table.

I have started to this code

select   
    month(date) month,
    isnull(sum(case when year(DATE) = 2015 then sales end), 0) as '2015'
from 
    tblSales
where 
    tenantcode = 'cmbina13'
group by  
    month(date)
order by 
    month(date)

and I get back this result:

enter image description here

The figures are correct. However I want to make month column turn into month name. Like this image below as desired output

enter image description here

Upvotes: 1

Views: 1891

Answers (2)

Gehan Fernando
Gehan Fernando

Reputation: 1267

SELECT      DATENAME(mm, date) AS MName
            ,ISNULL(SUM(CASE WHEN YEAR(GETDATE()) = 2015 then sales end), 0) AS '2015'
FROM        tblSales
WHERE       tenantcode = 'cmbina13'
GROUP BY    DATENAME(mm, date)
order by    DATENAME(mm, date)

Upvotes: 1

Pரதீப்
Pரதீப்

Reputation: 93694

Instead of Month function use Datename function with Month datepart

select  datename(month,date) [month]
       ,isnull(sum(case when year(DATE) = 2015 then sales end), 0) as '2015'
from tblSales
where tenantcode = 'cmbina13'
group by  datename(month,date)
Order by DATEPART(MM,datename(month,date)+' 01 2011')

Upvotes: 4

Related Questions