我的拆分功能有什么问题?

时间:2019-04-03 08:44:46

标签: sql sql-server tsql sql-server-2014-express

我写了split函数 'abc,def,ghi'这是我的输入,我想按逗号分割,但输出错误

您能解决这个问题

 alter function splitdata(
 @expression varchar(200),
 @seperator varchar(200)
 )
 RETURNS @SplitedData TABLE(
 splitcol varchar(1000)
 )
 AS
 BEGIN

 DECLARE @startIndex as int=1,@seperatorIndex as int    
 SET @seperatorIndex=CHARINDEX(@seperator,@expression,@startIndex)
 DECLARE @userData varchar(1000)

 WHILE @startIndex<@seperatorIndex
 BEGIN

 SET @userData=(SELECT
 SUBSTRING(@expression,@startIndex,@seperatorIndex-1))
 INSERT INTO @SplitedData VALUES(@userData)
 SET @startIndex=@seperatorIndex+1
 SET @seperatorIndex=CHARINDEX(@seperator,@expression,@startIndex)

 IF @startIndex>@seperatorIndex
 BEGIN

 SET @userData=(SELECT SUBSTRING(@expression,@startIndex,(SELECT 
 LEN(@expression))))
 INSERT INTO @SplitedData VALUES(@userData)

 END

 END
 RETURN

 END

输出:我正在得到这样的输出

abc

abc,def

ghi

0 个答案:

没有答案