Segregate Concatenated Column Values INTO New Columns Dynamically (SQL Server)

Hello Experts,

I need the "ServiceName" column values to be segregated into a new 3 columns dynamically as illustrated below :
As Is Table:
As-Is.PNG
To Be  table "Desired output":
ToBe.PNGThanks a lot.
HarreniAsked:
Who is Participating?
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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
SELECT ServiceID, ServiceName, 
    CASE PATINDEX('%AAA%', ServiceName)
        WHEN 0 THEN NULL
        ELSE 'AAA'
    END 'Has Service AAA?',
    CASE PATINDEX('%AAA%', ServiceName)
        WHEN 0 THEN NULL
        ELSE 'AAA'
    END 'Has Service AAA?',
    CASE PATINDEX('%BBB%', ServiceName)
        WHEN 0 THEN NULL
        ELSE 'BBB'
    END 'Has Service BBB?',
    CASE PATINDEX('%CCC%', ServiceName)
        WHEN 0 THEN NULL
        ELSE 'CCC'
    END 'Has Service CCC?'
FROM TableName

Open in new window

0
HarreniAuthor Commented:
Thanks a lot Vitor. What about if the ServiceName has "free text" input and want to have each value in a new column?
my table have around 4000 record and maximum values in each row will not exceed 8 values concatenated as maximum.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
For that you'll need a more dynamic solution and a place (mostly a table) where you can read the possible values ('AAA', 'BBB', etc...) that you'll need to compare with.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

HarreniAuthor Commented:
For the free text input, I don't need to compare it with any string. I just need to segregate each value in a new column, keeping in mind that I have around 4000 records and the maximum values concatenated are 200.

The yellow columns are the original table and and the green one are desired output.
Segregating-Details.PNGSegregating-Details.PNG
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
This still a very dynamic solution since you don't know how many words a string will have.
But if you have only a maximum of 4 words you can use PARSENAME function to accomplish this:
SELECT ServiceID, ServiceName, 
        PARSENAME(REPLACE(ServiceName,',','.'),1) Word1, 
        PARSENAME(REPLACE(ServiceName,',','.'),2) Word2, 
        PARSENAME(REPLACE(ServiceName,',','.'),3) Word3,
        PARSENAME(REPLACE(ServiceName,',','.'),4) Word4
FROM TableName

Open in new window

0
HarreniAuthor Commented:
Hi Vitor ,

The function above work with more than 4 words in each statement that I have,  but there are some statements that contains more than 80 words and cannot deal with it.
What is the alternative solution?

 Thanks a lot.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Harreni, I'm very sorry for the delay.
You can use a split function. If you don't like that one you can search for another version.
0

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
HarreniAuthor Commented:
Thanks a lot Vitor.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.