What about this solution then?
DECLARE @TourCodes VARCHAR(4000);
SET @TourCodes = ' AVG123 , JGH12,AVasfG123,JGsdfH12,AVsdgG123,JsdgGH12 , A34G123,J56gGH12, A34G1df23,JgfGH12';
with rcrs AS (
select rtrim(ltrim(LEFT(@TourCodes,charindex(',',@TourCodes)-1))) first,
SUBSTRING(@TourCodes+',',charindex(',',@TourCodes)+1,4000) other,
0 flg
union all
select rtrim(ltrim(LEFT(other,charindex(',',other)-1))) first,
SUBSTRING(other,charindex(',',other)+1,4000) other ,
flg+1 flg FROM rcrs WHERE charindex(',',other)>0
)
SELECT a.first one,b.first two from rcrs a
INNER JOIN rcrs b ON b.flg=a.flg+1 WHERE a.flg%2=0
In the recursive CTE the string is split starting from the beginning.
It might be noteworthy that I suffixed the original string in the first part of the recursive CTE by an extra ','
. This makes sure that all words are "eaten up" by the CTE and can then be processed by the main select using the modulo (%
) trick for determining which part goes into column one
or two
.
The result for this example is:
one two
AVG123 JGH12
AVasfG123 JGsdfH12
AVsdgG123 JsdgGH12
A34G123 J56gGH12
A34G1df23 JgfGH12
See here for a demo: http://rextester.com/WAA6224
Edit
I obviously had too much time on my hands ;-) ...
and so I went on to try out whether I could actually re-assemble the whole stuff into a ten-column collection the way the OP seems to want it. I am still not completely clear about how the result was meant to look, but here is another shot:
DECLARE @TourCodes VARCHAR(4000);
SET @TourCodes = REPLACE(REPLACE(
' AVG123 , JGH12,AVasfG123,JGsdfH12,AVsdgG123,JsdgGH12 , A34G123,J56gGH12, A34G1df23,JgfGH12,
AVG1234 , JGH126,AVasfG1238,JGsdfH122,AVsdgG1235,JsdgGH127 , A34G1239,J56gGH12a, A34G1df23c,JgfGH12e,
AVG1235 , JGH127,AVasfG1239,JGsdfH123,AVsdgG1236,JsdgGH128 , A34G1230,J56gGH12b, A34G1df23d,JgfGH12f',
char(10),''),char(13),''); -- this is just a slightly extended sample input string
with rcrs AS (
select rtrim(ltrim(LEFT(@TourCodes,charindex(',',@TourCodes)-1))) first,
SUBSTRING(@TourCodes+',',charindex(',',@TourCodes)+1,4000) other, 0 flg
union all
select rtrim(ltrim(LEFT(other,charindex(',',other)-1))) first,
SUBSTRING(other,charindex(',',other)+1,4000) other , flg+1 flg FROM rcrs WHERE charindex(',',other)>0 and flg<30
), cmbn AS (
SELECT a.flg/20 ii,(a.flg/2)%10 ij ,a.first+','+b.first tc FROM rcrs a
LEFT JOIN rcrs b ON b.flg=a.flg+1
WHERE a.flg%2=0
)
SELECT ii,'ABC' name,'Amazon' org, MAX(CASE ij WHEN 0 THEN tc END) tc1,
MAX(CASE ij WHEN 1 THEN tc END) tc2,
MAX(CASE ij WHEN 2 THEN tc END) tc3,
MAX(CASE ij WHEN 3 THEN tc END) tc4,
MAX(CASE ij WHEN 4 THEN tc END) tc5,
MAX(CASE ij WHEN 5 THEN tc END) tc6,
MAX(CASE ij WHEN 6 THEN tc END) tc7,
MAX(CASE ij WHEN 7 THEN tc END) tc8,
MAX(CASE ij WHEN 8 THEN tc END) tc9,
MAX(CASE ij WHEN 9 THEN tc END) tc10
FROM cmbn GROUP BY ii
The result looks like this:
ii name org tc1 tc2 tc3 tc4 tc5 tc6 tc7 tc8 tc9 tc10
0 ABC Amazon AVG123,JGH12 AVasfG123,JGsdfH12 AVsdgG123,JsdgGH12 A34G123,J56gGH12 A34G1df23,JgfGH12 AVG1234,JGH126 AVasfG1238,JGsdfH122 AVsdgG1235,JsdgGH127 A34G1239,J56gGH12a A34G1df23c,JgfGH12e
1 ABC Amazon AVG1235,JGH127 AVasfG1239,JGsdfH123 AVsdgG1236,JsdgGH128 A34G1230,J56gGH12b A34G1df23d,JgfGH12f
see here: http://rextester.com/ZJNV34690
Or, if you want all the ten Tourcodes in one columns you could do
SELECT ii,'ABC' name,'Amazon' org,
MAX(CASE ij WHEN 0 THEN tc END)+' '+
COALESCE(MAX(CASE ij WHEN 1 THEN tc END)+' ','')+
COALESCE(MAX(CASE ij WHEN 2 THEN tc END)+' ','')+
COALESCE(MAX(CASE ij WHEN 3 THEN tc END)+' ','')+
COALESCE(MAX(CASE ij WHEN 4 THEN tc END)+' ','')+
COALESCE(MAX(CASE ij WHEN 5 THEN tc END)+' ','')+
COALESCE(MAX(CASE ij WHEN 6 THEN tc END)+' ','')+
COALESCE(MAX(CASE ij WHEN 7 THEN tc END)+' ','')+
COALESCE(MAX(CASE ij WHEN 8 THEN tc END)+' ','')+
COALESCE(MAX(CASE ij WHEN 9 THEN tc END)+' ','') tourCodes
FROM cmbn GROUP BY ii
in the main select. See here http://rextester.com/AQCG28977
result:
ii name org tourCodes
0 ABC Amazon AVG123,JGH12 AVasfG123,JGsdfH12 AVsdgG123,JsdgGH12 A34G123,J56gGH12 A34G1df23,JgfGH12 AVG1234,JGH126 AVasfG1238,JGsdfH122 AVsdgG1235,JsdgGH127 A34G1239,J56gGH12a A34G1df23c,JgfGH12e
1 ABC Amazon AVG1235,JGH127 AVasfG1239,JGsdfH123 AVsdgG1236,JsdgGH128 A34G1230,J56gGH12b A34G1df23d,JgfGH12f