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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Month/Year format
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jpost
Starting Member

43 Posts

Posted - 03/19/2013 :  08:32:53  Show Profile  Reply with Quote
I am trying to pull a query that produces a result that will allow me to group by month and year. I am having trouble getting that part to work. I am using SQL 2008 and here is my query:

select count (*),year (to_char(a.date_field,'yyyy')),b.name
from table a
join table b on a.id = b.id
where a.date_field between to_char ('2011-jan','yyyy-mm') and to_char('2012-jan','yyyy-mm')
and a.age>= '65'
group by Year,b.name
order by b.name;

For some reason I get an error with what I am trying to do. I left the month part out for this example.
Thanks

Robowski
Posting Yak Master

101 Posts

Posted - 03/19/2013 :  08:55:10  Show Profile  Reply with Quote
will check again later from a computer if I can, but in the meantime using DATEPART should get you your desired result.

http://msdn.microsoft.com/en-us/library/ms174420.aspx
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8765 Posts

Posted - 03/19/2013 :  09:03:19  Show Profile  Visit webfred's Homepage  Reply with Quote
to_char is ORACLE?

Here we are on MS SQL Server only...


Too old to Rock'n'Roll too young to die.
Go to Top of Page

jpost
Starting Member

43 Posts

Posted - 03/19/2013 :  09:16:43  Show Profile  Reply with Quote
My appologies, I am working in both and sometimes accidently mix up oracle and sql
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8765 Posts

Posted - 03/19/2013 :  09:24:50  Show Profile  Visit webfred's Homepage  Reply with Quote
So you need to correct your question please so that anybody knows what you need or not need.


Too old to Rock'n'Roll too young to die.
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