实现字符串分隔
用于实现字符串的分隔,新建一张表返回
create FUNCTION SEPERATIONSTRING
(
@ARRAYSTR NVARCHAR(1000),--要分割的字符串
@SEPRATESYMBOL NVARCHAR(10)--分隔符
)
RETURNS @TEMPTABLE TABLE (X NVARCHAR(100))
AS
BEGIN
DECLARE @LI1 INT SET @LI1=1
DECLARE @LI2 INT SET @LI2=1
BEGIN
IF @SEPRATESYMBOL IS NULL OR @SEPRATESYMBOL=''
SET @SEPRATESYMBOL=','
SET @ARRAYSTR = @ARRAYSTR + @SEPRATESYMBOL
WHILE @LI1>0
BEGIN
SET @LI1 = CHARINDEX(@SEPRATESYMBOL,@ARRAYSTR,@LI2)
IF @LI1 = 0 BREAK
INSERT INTO @TEMPTABLE VALUES(SUBSTRING(@ARRAYSTR,@LI2-1,@LI1))
SET @ARRAYSTR=REPLACE(@ARRAYSTR,SUBSTRING(@ARRAYSTR,@LI2,@LI1),'')
END
END
RETURN
END
--执行
SELECT x FROM SEPERATIONSTRING('ASF,WER,SDFS,SD',',')
(
@ARRAYSTR NVARCHAR(1000),--要分割的字符串
@SEPRATESYMBOL NVARCHAR(10)--分隔符
)
RETURNS @TEMPTABLE TABLE (X NVARCHAR(100))
AS
BEGIN
DECLARE @LI1 INT SET @LI1=1
DECLARE @LI2 INT SET @LI2=1
BEGIN
IF @SEPRATESYMBOL IS NULL OR @SEPRATESYMBOL=''
SET @SEPRATESYMBOL=','
SET @ARRAYSTR = @ARRAYSTR + @SEPRATESYMBOL
WHILE @LI1>0
BEGIN
SET @LI1 = CHARINDEX(@SEPRATESYMBOL,@ARRAYSTR,@LI2)
IF @LI1 = 0 BREAK
INSERT INTO @TEMPTABLE VALUES(SUBSTRING(@ARRAYSTR,@LI2-1,@LI1))
SET @ARRAYSTR=REPLACE(@ARRAYSTR,SUBSTRING(@ARRAYSTR,@LI2,@LI1),'')
END
END
RETURN
END
--执行
SELECT x FROM SEPERATIONSTRING('ASF,WER,SDFS,SD',',')
结果: