Split text String based on cahracter

I need to split a field that contains "size | color" called [desc2] into 2 seprate fields of Size and Color in MS SQL 2005. The size and color are sperated by' | '. I have used patindex before but cant remeber how and believe there must be a better way.

XXLarge | Black                                            

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

Aneesh RetnakaranDatabase AdministratorCommented:

select rtrim( LEFT(desc2, charindex('|',desc2 )-1)) , ltrim(RIGHT (desc2, len(desc2)- charindex('|',desc2 ) ) )
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
BitsqueezerCommented:
Hi,

this one tests if the string contains this pattern before trying to convert it.

Cheers,

Christian

SELECT CASE WHEN PATINDEX('% | %',MyColumn) = 0 THEN '' ELSE  LEFT(MyColumn,              PATINDEX('% | %',MyColumn)-1) END AS [Size],
	   CASE WHEN PATINDEX('% | %',MyColumn) = 0 THEN '' ELSE RIGHT(MyColumn,LEN(MyColumn)-PATINDEX('% | %',MyColumn)-2) END AS [Color]

Open in new window

0
ubsmailAuthor Commented:
select rtrim( LEFT(desc2, charindex('|',desc2 )-1)) , ltrim(RIGHT (desc2, len(desc2)- charindex('|',desc2 ) ) )

This solution left the color column blank.
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

ubsmailAuthor Commented:
SELECT CASE WHEN PATINDEX('% | %',MyColumn) = 0 THEN '' ELSE  LEFT(MyColumn,              PATINDEX('% | %',MyColumn)-1) END AS [Size],
         CASE WHEN PATINDEX('% | %',MyColumn) = 0 THEN '' ELSE RIGHT(MyColumn,LEN(MyColumn)-PATINDEX('% | %',MyColumn)-2) END AS [Color]
 


This solution did the same.

the field is desc2 and the data is always size | color
0
ubsmailAuthor Commented:
I believe I know the issue but I am unclear how to fix it. the DESC2 is 70 cahrters so if we are using the RIGHT syntex the proper position would be 63 making it 70 -' | '  
0
ubsmailAuthor Commented:
select rtrim( LEFT(desc2, charindex('|',desc2 )-1)) as Size, rtrim(RIGHT(desc2, 60-charindex('|',desc2)-1)) as Color

This Worked thankyou
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 2005

From novice to tech pro — start learning today.