sql 分裂字符串函数

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

posted @ 2013-02-05 09:11  只想做好  阅读(1844)  评论(0编辑  收藏  举报