Loop string and update table

Could you show me how to split @username (by ',') and update table for each item in sql.

Declare @username varchar(100)
set @username = 'Jim, 'John, 'Dave'

Update table set username = ? where user = @username
VBdotnet2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VBdotnet2005Author Commented:
Any thoughts Experts?
0
sarabandeCommented:
you can have a where clause like

where user in ('Jim, 'John, 'Dave')

Open in new window


what probably is what you intend to do.

however the update table statement doesn't make any sense cause you rarely want to update the user name to a value it already has (since you searched for it).

Sara
0
x-menIT super heroCommented:
Declare @username varchar(100)
set @username = 'Jim, John, Dave'

declare @u varchar(max)
while charindex(',',@username) > 0
 begin
      set @u = substring(@username,0,charindex(',',@username))      
      SET @username = LTRIM(STUFF(@username,1,len(@u)+1,''))
      Update table set username = ? where user = @u
end
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

dsackerContract ERP Admin/ConsultantCommented:
Please provide more information.
0
aikimarkCommented:
your string looks funny
0
Craig WagnerSoftware ArchitectCommented:
Why is "Visual Basic.NET" one of the topics on this question? It appears to be a pure SQL question. Or are you looking to do it either in SQL or VB.NET?
0
VBdotnet2005Author Commented:
thanks x-men, this is exactly what I was looking for.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.