-- Преобразует строковый список в целочисленный столбец
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StringToIntList]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].StringToIntList
GO
CREATE FUNCTION StringToIntList
(
    @string VARCHAR(8000),
    @delimiter CHAR(1)
)
RETURNS @output TABLE(
    data int
)
BEGIN

    DECLARE @start INT, @end INT
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)

    WHILE @start < LEN(@string) + 1 BEGIN
        IF @end = 0 
            SET @end = LEN(@string) + 1

        INSERT INTO @output (data) 
        VALUES(CAST(SUBSTRING(@string, @start, @end - @start) as int))
        SET @start = @end + 1
        SET @end = CHARINDEX(@delimiter, @string, @start)
    END

    RETURN

END
