CREATE OR ALTER FUNCTION [dbo].[MIG_PLM_STRING_SPLIT_Fn]( @stringToSplit VARCHAR(MAX) , @sep VARCHAR(1))
RETURNS
@returnList TABLE ([value] [nvarchar] (max))
AS
BEGIN

DECLARE @name NVARCHAR(255)
DECLARE @pos INT

WHILE CHARINDEX(@sep, @stringToSplit) > 0
BEGIN
  SELECT @pos  = CHARINDEX(@sep, @stringToSplit)  
  SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)

  INSERT INTO @returnList 
  SELECT @name

  SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
END

INSERT INTO @returnList
SELECT @stringToSplit

RETURN
END


