CREATE FUNCTION [dbo].[SplitString] ( -- Add the parameters for the function here @SplitString nvarchar(max), @Separator nvarchar(10)=''
) RETURNS @result table ( [Id] int, [Text] nvarchar(500) ) AS Begin DECLARE @CurrentIndex int; DECLARE @NextIndex int; DECLARE @ReturnText nvarchar(500); SELECT @CurrentIndex=1; DECLARE @index int =0 WHILE(@CurrentIndex<=len(@SplitString)) BEGIN SELECT @NextIndex=charindex(@Separator,@SplitString,@CurrentIndex); IF(@NextIndex=0 OR @NextIndex IS NULL) SELECT @NextIndex=len(@SplitString)+1; SELECT @ReturnText=substring(@SplitString,@CurrentIndex,@NextIndex-@CurrentIndex); INSERT INTO @result([Id],[Text]) VALUES(@index,@ReturnText); SELECT @CurrentIndex=@NextIndex+1; select @index=@index+1; END return; End
GO