It looks like you are outdated when using CTE, so try the following:
DECLARE @YourTable table (RowID int, Layout varchar(200)) INSERT @YourTable VALUES (1,'hello,world,welcome,to,tsql') INSERT @YourTable VALUES (2,'welcome,to,stackoverflow') ;WITH SplitSting AS ( SELECT RowID,LEFT(Layout,CHARINDEX(',',Layout)-1) AS Part ,RIGHT(Layout,LEN(Layout)-CHARINDEX(',',Layout)) AS Remainder FROM @YourTable WHERE Layout IS NOT NULL AND CHARINDEX(',',Layout)>0 UNION ALL SELECT RowID,LEFT(Remainder,CHARINDEX(',',Remainder)-1) ,RIGHT(Remainder,LEN(Remainder)-CHARINDEX(',',Remainder)) FROM SplitSting WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)>0 UNION ALL SELECT RowID,Remainder,null FROM SplitSting WHERE Remainder IS NOT NULL AND CHARINDEX(',',Remainder)=0 ) SELECT * FROM SplitSting ORDER BY RowID
OUTPUT:
RowID Part ----------- ----------------------- 1 hello 1 world 1 welcome 1 to 1 tsql 2 welcome 2 to 2 stackoverflow (8 row(s) affected)
here's a great article on row splitting in SQL Server: βArrays and lists in SQL Server 2005 and Beyond when table value parameters are not cutβ by Erland Sommargog
EDIT here is another version (but you need a table of numbers) returns the same results as above:
;WITH SplitValues AS ( SELECT RowID,ListValue FROM (SELECT RowID, LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(',', List2, number+1)-number - 1))) AS ListValue FROM ( SELECT RowID, ',' + Layout + ',' AS List2 FROM @YourTable ) AS dt INNER JOIN Numbers n ON n.Number < LEN(dt.List2) WHERE SUBSTRING(List2, number, 1) = ',' ) dt2 WHERE ListValue IS NOT NULL AND ListValue!='' ) SELECT * FROM SplitValues
see number table: What is the best way to create and populate a number table?