DECLARE @csv varchar( 255 ) SET @csv = ',value1,value2,value3,value4,value5,'
SELECT SUBSTRING( @csv , n.Number + 1 , CHARINDEX( ',' , @csv , n.Number + 1 ) - n.Number - 1 ) AS Value FROM Numbers AS n WHERE n.Number BETWEEN 1 And LEN( @csv ) - 1 And SUBSTRING( @csv , n.Number , 1 ) = ','
SELECT Value , LEN( DelimValue ) - LEN( REPLACE( DelimValue , ',' , '' ) ) - 1 AS Position FROM ( SELECT SUBSTRING( @csv , n.Number + 1 , CHARINDEX( ',' , @csv , n.Number + 1 ) - n.Number - 1 ) , SUBSTRING( @csv , 1 , CHARINDEX( ',' , @csv , n.Number + 1 ) ) FROM Numbers AS n WHERE n.Number BETWEEN 1 And LEN( @csv ) - 1 And SUBSTRING( @csv , n.Number , 1 ) = ',' ) AS n( Value , DelimValue )
SELECT SUBSTRING( @csv , StartPos + 1 , EndPos - StartPos - 1 ) AS Value FROM ( SELECT n1.Number AS StartPos , MIN( n2.Number ) as EndPos FROM Numbers n1 JOIN Numbers n2 ON n2.Number > n1.Number WHERE n1.Number BETWEEN 1 And LEN( @csv ) - 1 And n2.Number BETWEEN 1 And LEN( @csv ) And SUBSTRING( @csv , n1.Number , 1 ) = ',' And SUBSTRING( @csv , n2.Number , 1 ) = ',' GROUP BY n1.Number ) AS n
SELECT SUBSTRING( @csv , StartPos + 1 , EndPos - StartPos - 1 ) as Value FROM ( SELECT n1.Number AS StartPos , ( SELECT MIN( n2.Number ) FROM Numbers n2 WHERE n2.Number BETWEEN 1 And LEN( @csv ) And SUBSTRING( @csv , n2.Number , 1 ) = ',' And n2.Number > n1.Number ) AS EndPos FROM Numbers n1 WHERE n1.Number BETWEEN 1 And LEN( @csv ) - 1 And SUBSTRING( @csv , n1.Number , 1 ) = ',' ) AS n |