SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE FUNCTION dbo.f_split_to_table (@text nvarchar(MAX), @delimiter nvarchar(32) = ',')
	RETURNS @table TABLE ( [position] INT IDENTITY PRIMARY KEY, item nvarchar(MAX) )   
AS

BEGIN
    DECLARE @xml XML
    

	if @text is not null 
	begin
		if len(@text) > 0
		begin

			SET @XML = N'<root><r>' + REPLACE(@text, @delimiter, '</r><r>') + '</r></root>'

			INSERT INTO @table(item)
			SELECT r.value('.','VARCHAR(MAX)') as Item
			FROM @xml.nodes('//root/r') AS RECORDS(r)

		end
	end

    RETURN
END
GO
