Please start any new threads on our new site at http://forums.sqlteam.com. 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
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Count Instances based on specific string
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

qman
Constraint Violating Yak Guru

USA
442 Posts

Posted - 05/08/2013 :  13:16:42  Show Profile  Reply with Quote
I have a column which contains the following values

colA
A
ABC, DEF
GHI, J
KLM, NOP, QRS, TUV

I want to count the number of times a value appears before and after a comma.

My desired result would be:


colA                    colB        
A                       1
ABC, DEF                2
GHI, J                  2
KLM, NOP, QRS, TUV      4 


Any help would be appreciated.....

James K
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 05/08/2013 :  13:24:14  Show Profile  Reply with Quote
SELECT ColA,
	LEN(colA) - LEN(REPLACE(colA,',','')) + 1 AS ColB
FROM
	YourTable;
If there is the possibility that there are leading and/or trailing comma's or if there are groups of comma's with nothing in between, and if you want to account for all of that, there is a little bit more work required.
Go to Top of Page

qman
Constraint Violating Yak Guru

USA
442 Posts

Posted - 05/08/2013 :  13:30:55  Show Profile  Reply with Quote
Nice, that works great!
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