Inserting Comma Separated values into table
inserting Comma separated values into table with the help of Charindex.
declare @CommaSeparatedValues varchar(50)
set @Values = '1,2,3,4,5,6';
declare @pos varchar(10)
----Add one more comma to the end of variable so while loop can read upto end.------
set @CommaSeparatedValues = @CommaSeparatedValues + ',';
while(Charindex( ',',@Values)>0)
Begin
SET @pos =Substring(@CommaSeparatedValues,1,Charindex(',',@CommaSeparatedValues)-1)
----- your Code to process on single value. I have just print it.----
print @pos
SELECT @CommaSeparatedValues= Substring(@CommaSeparatedValues,Charindex(',',@CommaSeparatedValues)+1,len(@CommaSeparatedValues))
End
No comments:
Post a Comment