While loop with comma separated values in sql server
While loop with comma separated values in sql server DECLARE @valueList varchar ( 8000 ) DECLARE @pos INT DECLARE @len INT DECLARE @value varchar ( 8000 ) SET @valueList = 'sam,delhi,kandy,' set @pos = 0 set @len = 0 WHILE CHARINDEX ( ',' , @valueList , @pos + 1 )> 0 BEGIN set @len = CHARINDEX ( ',' , @valueList , @pos + 1 ) - @pos set @value = SUBSTRING ( @valueList , @pos , @len ) PRINT @value set @pos = CHARINDEX ( ',' , @valueList , @pos + @len ) + 1 END