IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SplitString]') AND xtype in (N'FN', N'IF', N'TF'))
DROP FUNCTION [dbo].SplitString
GO
CREATE FUNCTION [dbo].[SplitString]
(
    @List NVARCHAR(MAX),
    @Delimiter CHAR(1)
)
RETURNS @Result TABLE (Value NVARCHAR(MAX))
AS
BEGIN
    DECLARE @Start INT, @End INT
    SET @Start = 1

    WHILE CHARINDEX(@Delimiter, @List, @Start) > 0
    BEGIN
        SET @End = CHARINDEX(@Delimiter, @List, @Start)
        INSERT INTO @Result (Value)
        VALUES (SUBSTRING(@List, @Start, @End - @Start))
        SET @Start = @End + 1
    END

    INSERT INTO @Result (Value)
    VALUES (SUBSTRING(@List, @Start, LEN(@List) - @Start + 1))

    RETURN
END
GO


