SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Administration
 Year and Month
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

United Kingdom
151 Posts

Posted - 07/02/2014 :  06:50:43  Show Profile  Reply with Quote

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
(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)

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.



Flowing Fount of Yak Knowledge

2684 Posts

Posted - 07/02/2014 :  07:41:37  Show Profile  Reply with Quote
try this:

select cast(year(getdate()) as char(4)) + ' ' + datename(month, getdate())
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000