select t2.id, t3.split, row_number() over (partition by t2.id order by t2.id) as num from ( select id, cast('' + replace( xmlfilter, ',', '') + '' as xml) xmlfilter from #testTable3 cross apply ( select '' + col1 for xml path('') ) t(xmlfilter) ) t2 cross apply ( select col1data.D.value('.','varchar(50)') as split from t2.xmlfilter.nodes('x') as col1data(D) ) t3and it will address the issue you have. The idea is to select the col1 values for xml path forcing the engine to entitize unfriendly characters BEFORE adding X nodes part and casting it as xml :)
create table #temp (id int, col1 varchar(512)); insert into #temp select 1, 'Nissan Dealer&>>>&&, 123 Main St, Chicago, IL, 76533' union all select 2, 'some place, 1234 Somewhere St, Stillater, OK, 123456'; ;with cte as ( select t2.id, t3.split, row_number() over ( partition by t2.id order by t2.id) as num from ( select id, cast('' + replace( xmlfilter, ', ', '') + '' as xml) xmlfilter from #temp cross apply ( select '' + col1 for xml path('') ) t(xmlfilter) ) t2 cross apply ( select item.value('.','varchar(50)') as split from t2.xmlfilter.nodes('x') as R(item) ) t3 ) select id, [1], [2], [3], [4], [5] from ( select id, num, split from cte ) as src pivot (max(split) for num in ([1], [2], [3], [4], [5])) as pvt drop table #temp;
1 Person is following this question.