dev-exchange.com logo

dev-exchange.com latest topics RSS feed

Book mark: Add http://www.dev-exchange.com/cms_view_article.php?aid=27&start=0&sid=71caa6b845d8249af8fbb0c72c05e2f2 to your delicious account Add http://www.dev-exchange.com/cms_view_article.php?aid=27&start=0&sid=71caa6b845d8249af8fbb0c72c05e2f2 to your digg account Add http://www.dev-exchange.com/cms_view_article.php?aid=27&start=0&sid=71caa6b845d8249af8fbb0c72c05e2f2 to your blinklist account Add http://www.dev-exchange.com/cms_view_article.php?aid=27&start=0&sid=71caa6b845d8249af8fbb0c72c05e2f2 to your reddit account Add http://www.dev-exchange.com/cms_view_article.php?aid=27&start=0&sid=71caa6b845d8249af8fbb0c72c05e2f2 to Dzone Add http://www.dev-exchange.com/cms_view_article.php?aid=27&start=0&sid=71caa6b845d8249af8fbb0c72c05e2f2 to your furl account Add http://www.dev-exchange.com/cms_view_article.php?aid=27&start=0&sid=71caa6b845d8249af8fbb0c72c05e2f2 to your stumble account Add http://www.dev-exchange.com/cms_view_article.php?aid=27&start=0&sid=71caa6b845d8249af8fbb0c72c05e2f2 to your Yahoo myweb account
Article Menu
Google
ColdFusion MX
PHP
ASP
ASP.Net
XML-XSLT
JavaScript
SQL Server
AJAX
Android
 


 
Article
Kishore
Contributing Member
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!
Rating: 0.00/5.00 [0]

Comments
No comments were made for this article
 

All times are GMT

Jump to:  
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


© 2008 dev-exchange.com
Powered by phpBB. Theme DEVPPL.