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?

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

x
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

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.
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.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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
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

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.
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.

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.
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.