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 Programming
 Month/Year format

Author  Topic 

jpost
Starting Member

43 Posts

Posted - 2013-03-19 : 08:32:53
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 - 2013-03-19 : 08:55:10
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
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-03-19 : 09:03:19
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 - 2013-03-19 : 09:16:43
My appologies, I am working in both and sometimes accidently mix up oracle and sql
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2013-03-19 : 09:24:50
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
   

- Advertisement -