现在的位置: 首页 > 综合 > 正文

sqlserver split函数

2013年04月30日 ⁄ 综合 ⁄ 共 464字 ⁄ 字号 评论关闭

ALTER FUNCTION  [dbo].[UFun_SqlSplit] (@Str varchar(120),@Sp varchar(120))
RETURNs @table TABLE(       
ID int IDENTITY PRIMARY KEY,     
value nvarchar(max)   
)
BEGIN
/*
Test:
select * from SqlSplit('123,234',',')
*/
IF LEN(@Str)<1
return;

 DECLARE @pos int
 DECLARE @temppos int
 SET @pos =0;
 WHILE(CHARINDEX(@Sp,@Str,0)>0)
BEGIN
SET @temppos =CHARINDEX(@Sp,@Str,0)
INSERT INTO @table VALUES(substring(@Str,1,@temppos-1))
SET @Str=substring(@Str,@temppos+1,len(@Str))

END
INSERT INTO @table VALUES(@Str)
RETURN
END

抱歉!评论已关闭.