title: Split string by comma in SQL Server 2008
body: <p>There are two strings <code>a</code> and <code>b</code></p>
  
  <p>The <code>a</code> string contains comma. I would like to split the a string by comma, then go through every element .</p>
  
  <p>IF the <code>b</code> string contains any element which split by comma will return 0</p>
  
  <p>(e.g: a = <code>"4,6,8"</code> ; b = <code>"R3799514"</code> because the <code>b</code> string contains 4 so return 0)</p>
  
  <p>How to achieve this with a stored procedure? Thanks in advance!</p>
  
  <p>I have seen a split function:</p>
  
  <pre><code>CREATE FUNCTION dbo.Split(@String varchar(8000), @Delimiter char(1))     
  returns @temptable TABLE (items varchar(8000))     
  as     
  begin     
  declare @idx int     
  declare @slice varchar(8000)     
  
  select @idx = 1     
      if len(@String)&lt;1 or @String is null  return     
  
  while @idx!= 0     
  begin     
      set @idx = charindex(@Delimiter,@String)     
      if @idx!=0     
          set @slice = left(@String,@idx - 1)     
      else     
          set @slice = @String     
  
      if(len(@slice)&gt;0)
          insert into @temptable(Items) values(@slice)     
  
      set @String = right(@String,len(@String) - @idx)     
      if len(@String) = 0 break     
  end 
  return     
  end
  
  select top 10 * from dbo.split('Chennai,Bangalore,Mumbai',',')
  </code></pre>