|
|
| Article |
Kishore
Contributing Member

Articles: 10 Comments: 3
|
|
|
| Posted: Thu May 15, 2008 11:09 am |
|
|
I had answered a question recently in dev-exchange forums about the looping through the comma separated list values in SQL server. I thought to publish this here so that it is useful others as well.
It uses charindex() to find poisition of comma within a string and then extract the values from it.
Here is the code:
| Code: | DECLARE @id INT
,@position INT
, @idlist varchar(8000);
SET @position=0;
SET @idlist='1,2,3,4,5,6,7';
-- add a comma in the end to make the list loopable
if (right(@idlist,1)<>',') SET @idlist=@idlist+',';
WHILE charindex(',',@idlist)>0
BEGIN
SET @id = cast(substring(@idlist,0, charindex(',',@idlist)) as int)
print @id;
SET @idlist = substring(@idlist, charindex(',',@idlist)+1, LEN(@idlist) - @position);
END |
have a good programming! |
|
|
| Comments |
| No comments were made for this article |
| |
|
All times are GMT
You cannot post articles in this chapter You cannot edit your articles in this chapter You cannot delete your articles in this chapter You cannot rate articles in this chapter
You cannot post comments in this chapter You cannot edit your comments in this chapter You cannot delete your comments in this chapter You cannot rate comments in this chapter
|
|