如何拆分字段中1|2|3|4这样的字符串变成多个字段值
请问各位高人:
在mssql2000中,一个表中有org_path字段,这个字段中的数据类似这样:
org_path
---------------------------
1
1|2
1|28
1|31|124
1|31|124|192
1|5|134|156|168
最小值是 "1 ",最大的值如 1|5|134|156|168 ,包含5个值,4个|
现在我是想把org_path这字段中的值拆分成5个字段,如下:
org_path a b c d e
------------------------
1 1
1|2 1 2
1|28 1 28
1|31|124 1 31 124
1|31|6|192 1 31 6 192
1|5|134|156|168 1 5 134 156 168
请问,如果实现如上所需要的,应该怎么写SQL,请各位指导,多谢!
------解决方案--------------------declare @t table(org_path varchar(20))
insert into @t select '1 '
insert into @t select '1|2 '
insert into @t select '1|28 '
insert into @t select '1|31|124 '
insert into @t select '1|31|124|192 '
insert into @t select '1|5|134|156|168 '
select
a,
reverse(parsename(b,1)) as b,
reverse(parsename(b,2)) as c,
reverse(parsename(b,3)) as d,
reverse(parsename(b,4)) as e
from
(select
a,
reverse(replace(case when b= ' ' then b else left(b,len(b)-1) end , '| ' , '. ')) as b
from
(select
left(org_path,charindex( '| ',org_path+ '| ')-1) as a,
stuff(org_path+ '| ',1,charindex( '| ',org_path+ '| '), ' ') as b
from
@t
) s
)t
/*
a b c d e
---- ---- ---- ---- ----
1 NULL NULL NULL NULL
1 2 NULL NULL NULL
1 28 NULL NULL NULL
1 31 124 NULL NULL
1 31 124 192 NULL
1 5 134 156 168
*/
------解决方案--------------------学习,如果第一级是1
select 1,reverse(PARSENAME(A,1)),reverse(PARSENAME(A,2)),reverse(PARSENAME(A,3)),reverse(PARSENAME(A,4)) from
(select replace(reverse(stuff(org_path,1,2, '