谁能帮帮我?
我有这样一列
Column
John > Doe Doe > Writer
Jane > Doe > Chemical Engineer > Sovena
我希望我的最终结果是
**Column** **Column2** **Column3** **Column4**
John Doe Doe Writer
Jane Doe Chemical Engineer Sovena
我需要这个在SQL中,用"> "分割,你能帮忙吗?"谢谢!"。
这回答了问题的原始版本。
不幸的是,内置的字符串拆分功能并没有提供组件的索引。 所以,一个解决方案是递归子查询。
with cte as (
select id, convert(varchar(max), NULL) as str,
convert(varchar(max), str) as rest, 0 as lev
from t
union all
select id, left(rest, charindex(' > ', rest + ' > ') - 1),
stuff(rest, 1, charindex(' > ', rest + ' > ') + 2, ''),
lev + 1
from cte
where rest <> ''
)
select max(case when lev = 1 then str end),
max(case when lev = 2 then str end),
max(case when lev = 3 then str end),
max(case when lev = 4 then str end)
from cte
group by id;
这里 是一个db<>提琴。