Comma Delimited String

I have a string in SYBASE 11.5x that varies in length at any given time overall.  As well, the individual values in the string can vary in length also.  The only thing that remains the same is the comma delimiting between values.  For example, one string may read
(John, Dave,Betty) and the next read (Joe, Brian), etc.  How can I strip each value out the string between the commas to assign them to variables??
wcampb_ellAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ahoorConnect With a Mentor Commented:
All you can do is use a while statement.
But you won't be able to have all the values in variables at the same time.
So, open a while loop, fill a variable with a value, then do something with it, then go back in the loop and overwrite the variable with the new value.

Example:

declare ...

select @string = 'John,Dave,Betty'
,        @comma = 0

while @comma > 0
begin
     select @comma = charindex(',',@string)
     
     if @comma > 0 -- comma exists
     begin
        select @name = substring(@string,1, @comma -1)
        select @string = substring(@string,@comma + 1, 255)
    end
    else      -- last time
    begin
        select @name = @string
   end

   -- now do something with @name

end

Arjan
0
 
wcampb_ellAuthor Commented:
Edited text of question.
0
 
wcampb_ellAuthor Commented:
I thought this was the way to do it, but wasn't sure - your answer just solidifies my decision to run a loop!!
0
All Courses

From novice to tech pro — start learning today.