I need to retrieve the content in position 10 of a comma separated string in a View table.
Row 1 N,l,S,T,A,,<all>,,N,A,N,N,N,Y,Y,,Y,Y,Y,,AA,SA,Enterprise,
Row 2 M,,A,S,AS,SS,AS,N,N,N,N,Y,Y,Y,ENTERPRISE,S,,A
Row 3 L,,A,D,S,A,A,AA,Y,Y,Y,YNN,N,N,N,N,A,AA,AD,D,D
Div1 is the name of my column, Div2 is the name of the result column. I use the following code:
SELECT TOP (2000)
[Id],
CONVERT(XML,'<x>' + REPLACE(REPLACE(REPLACE(Div1, '>', ''), '<', ''), ',', '</x <x>') + '</x>').value('/x[10]', 'VARCHAR(MAX)') [Div2],
Div1
FROM
[dbo].[database]
I use character type VARCHAR(MAX) because that is the type for Div1 in my database. The code works if I run less than 20000 rows. But the data set I use has more than 100,000 rows. If I run the whole data it stops and the following error occurs:
Msg 9421, Level 16, State 1, Line 1.
XML parsing: line 1, character 218, illegal name character
Is there a way to work this around?