Technical Article

Split String and return a table of results

,

Takes a simple string of delimited values, and returns a table type of the values stripped out.  This can be easily incorporated in existing SQL statements utilising standard joins.  Classic example of a UDF returning the TABLE data type.

CREATE FUNCTION dbo.SplitString
(
  @List  varchar(500),
  @Delim varchar(10)
)
RETURNS 
@ParsedList table
(
  ListItem varchar(255)
)
AS
/*----------------------------------------------------------------------------------------------
' Function       : SplitString
'
' Description    : Split a string using the passed delimeter - returns a Table variable
'
' Change History :
'
' WHEN        WHO  WHAT
'----------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------*/BEGIN
  
  DECLARE @ListItem  varchar(255)
  DECLARE @Pos       int

  -- Ensure we have a trailing delimiter
  IF RIGHT(@List,LEN(@Delim)) <> @Delim
    SET @List = LTRIM(RTRIM(@List))+ @Delim

  SET @Pos = CHARINDEX(@Delim, @List, 1)

  IF REPLACE(@List, @Delim, '') <> ''
  BEGIN

    -- Break up the string
    WHILE @Pos > 0
    BEGIN
      
      SET @ListItem = LTRIM(RTRIM(LEFT(@List, @Pos - 1)))
      
      IF @ListItem <> ''
        INSERT INTO @ParsedList (ListItem) VALUES (@ListItem)

      SET @List = RIGHT(@List, LEN(@List) - @Pos - LEN(@Delim) + 1)
      SET @Pos = CHARINDEX(@Delim, @List, 1)

    END

  END
  RETURN

END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating