我在表中有一个字符串,需要将其拆分为不同的列,这是我的字符串;
"VEX_NAME=LILIAN BRIGHT||VEX_ID=3006030"
我想按以下格式拆分
VEX_ID | VEX_NAME
--------+----------------
3006030 | LILIAN BRIGHT
答案 0 :(得分:4)
您可以使用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;
但是,您也可以使用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);