You can use string_split()
select s.*
from t cross apply
(select max(case when seqnum = 1 then value end) as num,
max(case when seqnum = 2 then value end) as index,
max(case when seqnum = 3 then value end) as year,
max(case when seqnum = 4 then value end) as curr,
max(case when seqnum = 5 then value end) as val
from (select s.value,
row_number() over (order by charindex('|' + s.value + '|', '|' + t.datatextraw + '|') as seqnum
from string_split(t.datatextraw, '|') s
) s
) s;
Rumor has it that you can use order by (select null)
instead of the strange charindex()
expression because the latest versions of SQL Server return the values in order. However, until the documentation changes, I won't recommend that approach.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…