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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Count Instances based on specific string
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

qman
Constraint Violating Yak Guru

USA
439 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

3578 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
439 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  
 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