The Below SQL Function can be used to Insert comma Separated values into Table
/****** Object: UserDefinedFunction [dbo].[CommaSplit] Script Date: 11/07/2010 15:14:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Lokeshkumar
-- Create date: 11/07/2010
-- Description: Return Comma Separated Strings as DataTable
-- =============================================
ALTER FUNCTION [dbo].[CommaSplit]
(
@InputString VARCHAR(MAX)
)
RETURNS
@OutputTable TABLE
(
-- Add the column definitions for the TABLE variable here
Val VARCHAR(MAX)
)
AS
BEGIN
-- Fill the table variable with the rows for your result set
DECLARE @Val VARCHAR(MAX),@Pos INT,@Len INT
SET @Len = LEN(@InputString)
SET @Pos=0
WHILE (CHARINDEX(',',@InputString,@Pos)-@Pos) > 0
BEGIN
SET @Val = SUBSTRING(@InputString,@Pos,(CHARINDEX(',',@InputString,@Pos)-@Pos))
INSERT INTO @OutputTable(Val)
SELECT @Val
SET @Pos = CHARINDEX(',',@InputString,@Pos)+1
END
IF @Pos <= @Len
BEGIN
SET @Val = SUBSTRING(@InputString,@Pos,(@Len+1)-@Pos)
INSERT INTO @OutputTable(Val)
SELECT @Val
END
RETURN
END