На тот случай, если вы не хотите (или не можете использовать) это SUPER DelimitedSplit8K_LEAD
, вот подход XML, который будет поддерживать последовательность
Пример
Declare @S varchar(max) = 'A|B|C|D'
;with cte as (
Select RetSeq = row_number() over (order by 1/0)
,RetVal = ltrim(rtrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>' + replace((Select replace(@S,'|','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
), cte1 as (
Select *
,Comb ='['+stuff((select '|' +RetVal From cte Where RetSeq<=A.RetSeq Order By RetSeq For XML Path ('')),1,1,'') +']'
From cte A
Group By RetSeq,RetVal
)
Select NewValue = stuff((select ',' +Comb From cte1 Order By RetSeq For XML Path ('')),1,1,'')
Возвращает
NewValue
[A],[A|B],[A|B|C],[A|B|C|D]