Tuesday, March 22, 2011

My Coding Experience.: Inserting Comma Separated values into table


Inserting Comma Separated values into table

inserting Comma separated values into table with the help of Charindex.

----I have declared Variable for Comma separated values. -------


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
Post a Comment

Find a cool free stuff everyday

Giveaway of the Day

Hiren Bharadwa's Posts

DotNetJalps