Use below function to split the comma separated string value in sql server.
to test use commented sql query
--select SplittedData from SplitStrings('test1.txt,test2.txt',',')
CREATE FUNCTION SplitStrings
(
@SplitData nvarchar(max),
@SplitCharacter nvarchar(5)
)
RETURNS @ReturnValue table
(
UniqueId int identity(1,1),
SplittedData nvarchar(100)
)
AS
BEGIN
Declare @Count int
Set @Count = 1
While (Charindex(@SplitCharacter,@SplitData)>0)
Begin
Insert Into @ReturnValue(SplittedData)
Select
SplittedData = ltrim(rtrim(Substring(@SplitData,1,Charindex(@SplitCharacter,@SplitData)-1)))
Set @SplitData = Substring(@SplitData,Charindex(@SplitCharacter,@SplitData)+1,len(@SplitData))
Set @Count = @Count + 1
End
Insert Into @ReturnValue (SplittedData)
Select SplittedData = ltrim(rtrim(@SplitData))
Return
END
to test use commented sql query
--select SplittedData from SplitStrings('test1.txt,test2.txt',',')
CREATE FUNCTION SplitStrings
(
@SplitData nvarchar(max),
@SplitCharacter nvarchar(5)
)
RETURNS @ReturnValue table
(
UniqueId int identity(1,1),
SplittedData nvarchar(100)
)
AS
BEGIN
Declare @Count int
Set @Count = 1
While (Charindex(@SplitCharacter,@SplitData)>0)
Begin
Insert Into @ReturnValue(SplittedData)
Select
SplittedData = ltrim(rtrim(Substring(@SplitData,1,Charindex(@SplitCharacter,@SplitData)-1)))
Set @SplitData = Substring(@SplitData,Charindex(@SplitCharacter,@SplitData)+1,len(@SplitData))
Set @Count = @Count + 1
End
Insert Into @ReturnValue (SplittedData)
Select SplittedData = ltrim(rtrim(@SplitData))
Return
END