.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson
Eminent IT

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Spliting Comma (,) in SQL Query

Posted By:Shashi Ray       Posted Date: August 30, 2011    Points: 40    Category: SQL Query    URL: http://www.dotnetspark.com  

How to seperate values with the help of Comma (,)

How to seperate values with the help of Comma (,)

declare @strroles varchar(10)
declare @pos int

set @strroles = '1,2,3,4'; --Example Values

if substring(@strroles, LEN(@strroles)-1,0)<>','
 set @strroles=@strroles+','

set @pos=0

declare @roleid as int
 while charindex(',',@strroles)>0

set @roleid = cast(substring (@strroles,0,charindex(',',@strroles))as int)

 select @roleid

set @strroles= substring(@strroles, charindex (',',@strroles)+1, len(@strroles)-@pos)



Shashi Kant Ray


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend