Print Comma Seperated Values In SQL Server

Self-Innovator
Posted by Self-Innovator under Sql Server category on | Points: 40 | Views : 971
DECLARE @Test VARCHAR(1000), @RecSet VARCHAR(500)
DECLARE @StartingPos INT,@RecPos INT
SET @StartingPos= 1
SET @Test = '1,2,3,'
WHILE @StartingPos<=LEN(@Test)
BEGIN
SELECT @RecPos = CHARINDEX(',',@Test,@StartingPos)
SELECT @RecSet=SUBSTRING(@Test,@StartingPos,@RecPos-@StartingPos)
PRINT @RecSet
SELECT @StartingPos=@RecPos+1
END

Comments or Responses

Posted by: kgovindarao523-21772 on: 11/20/2014 Level:Bronze | Status: [Member] [MVP] | Points: 10
Hi,

Its not woking for "1,2,3" or ",1,2,3". rather its going to coninues loop.
Please make it worked for different scenarios.

Login to post response