Click here to Skip to main content
15,124,736 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:

I have this string
Declare @x nvarchar(10) = '1|2:1|3|4:2'
I want to divide these values between two strings

StringOne='1,2,3,4' //here i took each number before semi coloum
StringTwo= '1,2' //here i took each number after semi colon

What I have tried:

How can i split the first string into two strings.. I should split by '|' and ','
Updated 12-Aug-21 22:44pm
OriginalGriff 13-Aug-21 1:56am
What have you tried?
Where are you stuck?
What help do you need?

And more importantly - why are you trying to do this in SQL?

Use the "Improve question" widget to edit your question and provide better information.
Richard Deeming 13-Aug-21 4:10am
Your description says you take each number before/after a semi-colon. But your sample string doesn't include any semi-colons!

1 solution

If you are going to do this in SQL (I would do it in my UI layer) then use the CHARINDEX[^] to locate the position of the character then use SUBSTRING[^] to separate out the strings you want.

Or, you could use STRING_SPLIT [^]

Or you could read this article Splitting Strings Based on Patterns – SQLServerCentral[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900