SQL function to split string using separator(comma separated string)

CREATE FUNCTION [dbo].[fnSplit](@input NVARCHAR(MAX), @separator NVARCHAR(10)) RETURNS @tbl TABLE (Value NVARCHAR(max)) AS BEGIN DECLARE @xml XML; SELECT @xml = CAST('<input>' + REPLACE(@input, @separator, '</input><input>') + '</input>' AS XML); INSERT INTO @tbl(Value) SELECT Temp.split.value('.', 'NVARCHAR(max)') AS Value FROM @xml.nodes('/input') Temp(split) RETURN END

Output