title: SQL UDF split words and return to table
body: <pre><code>CREATE FUNCTION [dbo].[Split]
  (   
      @String NVARCHAR(4000),
      @Delimiter NCHAR(1)
  )
  RETURNS TABLE
  AS
  RETURN
  (
      WITH Split(stpos,endpos)  AS
  (        
  SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos       
  UNION ALL        
  SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1)          
  FROM Split            
  WHERE endpos &gt; 0   
  )   
  SELECT 'Data' = SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)
  FROM Split
  )
  GO
  </code></pre>
  
  <p>How do I spilt the words including commas, periods and spaces? At the moment it is just splitting where the commas are.</p>