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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

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

Posting Yak Master

United Kingdom
152 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

2780 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  
 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.02 seconds. Powered By: Snitz Forums 2000