CREATE FUNCTION [dbo].[SplitListMax] (@splitlist VARCHAR(max)) 
RETURNS @table TABLE (value VARCHAR(max)) AS
BEGIN

  /*
    Created by Kent Johansen January 2009
    Notes: Splits a comma sep. string into records in a table
    Changes:
    2015-05-07/SG: New function using varchar(max). Allows empty fields
  */

  SET @splitlist = RTRIM(LTRIM(@splitlist))

  DECLARE @nextcharindex INT
  SET @nextcharindex = CHARINDEX(',', @splitlist, 1) 

  WHILE 0 < @nextcharindex
  BEGIN
    INSERT @table SELECT SUBSTRING(@splitlist, 1, @nextcharindex-1)
    SET @splitlist = RTRIM(LTRIM(SUBSTRING(@splitlist, @nextcharindex+1, len(@splitlist))))
    SET @nextcharindex = CHARINDEX(',', @splitlist, 1) 
  END
  INSERT @table SELECT SUBSTRING(@splitlist, 1, len(@splitlist))

  RETURN 
END