SELECT ID FROM dbo.SplitIds('1,2,3,4,5')
CREATE FUNCTION [dbo].[SplitIds](
@identities varchar(2000)
)
RETURNS
@IdList table( ID int )
AS
BEGIN
DECLARE @ID varchar(10), @idx int
SELECT @identities = LTRIM(RTRIM(@identities))+ ','
SELECT @idx = CHARINDEX(',', @identities, 1)
IF REPLACE(@identities, ',', '') <> ''
BEGIN
WHILE @idx > 0
BEGIN
SELECT @ID = LTRIM(RTRIM(LEFT(@identities, @idx - 1)))
IF @ID <> ''
BEGIN
INSERT INTO @IdList (ID)
VALUES (CAST(@ID AS int))
END
SET @identities = RIGHT(@identities, LEN(@identities) - @idx)
SET @idx = CHARINDEX(',', @identities, 1)
END
END
RETURN
END