SQLServer Split


ALTER FUNCTION dbo.splitl (
    @String VARCHAR(MAX),
    @Delimiter VARCHAR(MAX)
) RETURNS @temptable TABLE (items VARCHAR(MAX)) AS
BEGIN
    DECLARE @idx INT=1
    DECLARE @slice VARCHAR(MAX) 
    IF LEN(@String) < 1 OR LEN(ISNULL(@String,'')) = 0
        RETURN
    WHILE @idx != 0
    BEGIN
        SET @idx = CHARINDEX(@Delimiter,@String)

        IF @idx != 0
            SET @slice = LEFT(@String,@idx - 1)
        ELSE
            SET @slice = @String

        IF LEN(@slice) > 0
            INSERT INTO @temptable(items) VALUES(@slice)

        SET @String = RIGHT (@String, LEN(@String) - @idx)

        IF LEN(@String) = 0
            BREAK
    END
    RETURN
END
SQL