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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Counting occurrences of a value in a string

Author  Topic 

evanburen
Posting Yak Master

167 Posts

Posted - 2009-03-11 : 14:17:33
Hi

I have a field in my database named divOrder which contains values like below and I want to count the number of times each one occurs. For example, I want the result to be something like Div1 20, Div2 8, Div3 12 etc. Possible values are Div1-Div15.

DivOrder (varchar(250)
Div5,Div6
Div5,Div6
Div5,Div6
Div6,Div10,Div13,Div16
Div3,Div5,Div6,Div9,Div10

Thank you

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-11 : 15:41:53
Can one row have more than 1 Div1 or Div2 etc?
Go to Top of Page

evanburen
Posting Yak Master

167 Posts

Posted - 2009-03-11 : 15:56:53
No, there could be only one of each. Thanks.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-11 : 16:03:11
Try this..

select sum(case charindex('Div1',divorder) when 0 then 0 else 1 end) as Div1Count,
sum(case charindex('Div2',divorder) when 0 then 0 else 1 end) as Div2Count,
sum(case charindex('Div3',divorder) when 0 then 0 else 1 end) as Div3Count,
sum(case charindex('Div4',divorder) when 0 then 0 else 1 end) as Div4Count
and so on till 15
from urtable
Go to Top of Page

evanburen
Posting Yak Master

167 Posts

Posted - 2009-03-11 : 16:09:43
That's exactly what I want. Thanks very much!
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-03-11 : 16:47:53
Welcome
Go to Top of Page
   

- Advertisement -