SQL Query for multiple value (comma separated) field

I have a survey table for which I want to count all of the "reasons" a customer is not buying from us.  

Two main columns and samples are:

Survey_ID : ReasonBuying

1 : Price, Other
2 : Price, Service, Quality
3 : Service, Relationship

I want to create a report that counts each response, so how do I get these columns to:

1 : Price
1 : Other
2 : Price
2 : Service
2 : Quality
3 : Service
3 : Relationship

Please help and thank you.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
Use a Split function.  There are many different versions out there but below is one that I use sometimes.

SELECT Split.Value AS Reason
FROM Survey AS S
CROSS APPLY dbo.SplitText(S.ReasonBuying, ',') AS Split

Open in new window

CREATE FUNCTION [dbo].[SplitText]
	@Delimiters VARCHAR(10)
RETURNS @Words TABLE (      
	Position INT,       
	Value VARCHAR(MAX)   
	SET @Text = RTRIM(@Text) 
	--DECLARE @Delimiters VARCHAR(10)    
	--SET @Delimiters = ' ,:/-' 
	DECLARE @Position INT, @i INT, @s VARCHAR(MAX)    
	SET @Position = 1     
	WHILE @Position < LEN(@Text)       
		AND CHARINDEX(SUBSTRING(@Text, @Position, 1), @Delimiters) > 0       
		SET @Position = @Position + 1     
		WHILE @Position <= LEN(@Text)    
			SET @i = PATINDEX('%[' + @Delimiters + ']%',          
				SUBSTRING(@Text, @Position, len(@Text) - @Position + 1))       
			IF @i > 0       
				SET @i = @i + @Position - 1          
				IF @i > @Position          
					-- @i now holds the earliest delimiter in the string            
					SET @s = SUBSTRING(@Text, @Position, @i - @Position)            
					INSERT INTO @Words             
					VALUES (@Position, @s)         
				SET @Position = @i + 1           
				WHILE @Position < LEN(@Text)             
					AND CHARINDEX(SUBSTRING(@Text, @Position, 1), @Delimiters) > 0             
					SET @Position = @Position + 1       
				SET @s = SUBSTRING(@Text, @Position, LEN(@Text) - @Position + 1)         
				INSERT INTO @Words          
				VALUES (@Position, @s) SET @Position = LEN(@Text) + 1      

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
akscottAuthor Commented:
Hi Brian; thank you for taking the time.  I've never actually used a function!  Had to do some reading on that -- I'm mostly just a query and report kind of person.  So, after doing some reading, I ran the script to create the function, then I tried customizing what you did in the top window to how I thought it fit with my stuff and I just get an error message.  I think I'm missing the link in my brain as to how the function actually interacts with the query to convert the comma separated field.  Can you tell me where I've gone wrong here:

Brian CroweDatabase AdministratorCommented:
Gonna do a little trouble-shooting.  Run this and tell me if it works.

SELECT * FROM dbo.SplitText('a,b,c,d,e,f', ',')
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

akscottAuthor Commented:
Query Result
Brian CroweDatabase AdministratorCommented:
That shows that the function is working correctly.  verify the datatype of sysdba.accountroomsurvey.ReasonBuying.  Also you will want to select Split.Value instead of ReasonBuying.
akscottAuthor Commented:
Aha, now we're getting somewhere.  I added an LTrim to remove the spaces that were showing up in the split results and it looks like I'm off to the races. THANK YOU, Brian.
Vitor MontalvãoMSSQL Senior EngineerCommented:
akscott, do you still need help with this question?
akscottAuthor Commented:
Thank you! Very helpful.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.