0

I have a string in a table that I need to split into different columns, this is my string;

"VEX_NAME=LILIAN BRIGHT||VEX_ID=3006030"

I would like to be split in the format below

  VEX_ID  |  VEX_NAME
  --------+----------------
  3006030 | LILIAN BRIGHT
2
  • Which version of SQL Server are you using - it will be relevant to this answer Commented Apr 10, 2019 at 10:39
  • Sql server 2012 Commented Apr 10, 2019 at 11:01

1 Answer 1

4

You can use substring() & charindex() :

select substring(col, charindex('VEX_ID=', col) + 7, len(col)) as VEX_ID,
       substring(col, charindex('VEX_NAME=', col) + 9, 
                 charindex('||', col) - (charindex('VEX_NAME=', col) + 9 )) as VEX_NAME
from table t;

However, you can also use APPLY :

select substring(col, VEX_ID, len(col)) as VEX_ID,
       substring(col, VEX_NAME, pipe - VEX_NAME) as VEX_NAME
from table t cross apply 
    ( values (charindex('VEX_ID=', col) + 7, charindex('VEX_NAME=', col) + 9,  charindex('||', col))
    ) tt(VEX_ID, VEX_NAME, pipe);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.