Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Year and Month

Author  Topic 

satheesh
Posting Yak Master

152 Posts

Posted - 2014-07-02 : 06:50:43
Hi,

I have to calculate total policies sold per month. I wrote the below query and i am getting the output like below but i need to display the month in the format year and month (2013 January)

select dateadd(month, datediff(month, 0, Policy .DateCreated ), 0) as Month, COUNT(policy.PolicyNumber) as Count
from Policy
where
(policy.datecreated between '2013-01-01 00:00:00.000' AND '2013-06-30 23:59:59.997')
GROUP BY dateadd(month, datediff(month, 0, Policy .DateCreated), 0)
order by dateadd(month, datediff(month, 0, Policy .DateCreated), 0)

Output
Month Count
2013-01-01 00:00:00.000 4153
2013-02-01 00:00:00.000 3904
2013-03-01 00:00:00.000 7854
--
--

But i need to display as

Month Count
2013 January 4153
2013 February 3904
--
--
i.e Month column in year and month format. How to modify the above query. Any help will be highly appreciated.

Thanks

Regards,
SG

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-07-02 : 07:41:37
try this:


select cast(year(getdate()) as char(4)) + ' ' + datename(month, getdate())
Go to Top of Page
   

- Advertisement -