Need a query to get strings seperated by commas in a column

I need to seperate strings seperated by commas

Example : I have a column A which has values (king,prince,queen,soldier)

I need to seperate these values and store it in a temp table.

How can I acheive this
Gowtham RamamoorthyAsked:
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.

Scott PletcherSenior DBACommented:
You need a splitter function.  The best one I know of dbo.DelimitedSplit8K.  I can't post it because of filters at work thinking it's "injection" -- grrrrr, so frustrating -- but you can Google to get the code for it easily enough.

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
ste5anSenior DeveloperCommented:
Use a split function or intermediate XML parsing;

DECLARE @Sample TABLE ( Payload VARCHAR(255) );

VALUES	( 'king,prince,queen,soldier');

WITH Data AS ( 
	SELECT CAST( '<z>' + REPLACE(S.Payload, ',', '</z><z>') + '</z>' AS XML ) AS d
	FROM @Sample S
SELECT	z.value('.', 'VARCHAR(255)')
	CROSS APPLY D.d.nodes('z')  Z ( z );

Open in new window

Éric MoreauSenior .Net ConsultantCommented:
ste5anSenior DeveloperCommented:
Maybe ;)

But I'd prefer a split function. It should be part of a devs toolkit, like a number tally table. But having that idea in mind is also nice :)
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.