在存储过程中拆分字符串

时间:2014-08-28 08:12:15

标签: sql sql-server

我有两个字符串变量:

@str1='abc/xyz'
@str2='/'

我希望使用这两个变量来提取'xyz'的{​​{1}}部分。

2 个答案:

答案 0 :(得分:0)

创建下表值函数:

您将获得像

这样的价值观
select * from dbo.fn_split(@str1,@str2)

CREATE FUNCTION [dbo].[fn_Split](@sText varchar(8000), @sDelim varchar(20) = ' ')
RETURNS @retArray TABLE (idx smallint Primary Key, value varchar(8000))
AS
BEGIN
DECLARE @idx smallint,
    @value varchar(8000),
    @bcontinue bit,
    @iStrike smallint,
    @iDelimlength tinyint

IF @sDelim = 'Space'
    BEGIN
    SET @sDelim = ' '
    END

SET @idx = 0
SET @sText = LTrim(RTrim(@sText))
SET @iDelimlength = DATALENGTH(@sDelim)
SET @bcontinue = 1

IF NOT ((@iDelimlength = 0) or (@sDelim = 'Empty'))
    BEGIN
    WHILE @bcontinue = 1
        BEGIN

--If you can find the delimiter in the text, retrieve the first element and
--insert it with its index into the return table.

        IF CHARINDEX(@sDelim, @sText)>0
            BEGIN
            SET @value = SUBSTRING(@sText,1, CHARINDEX(@sDelim,@sText)-1)
                BEGIN
                INSERT @retArray (idx, value)
                VALUES (@idx, @value)
                END

--Trim the element and its delimiter from the front of the string.
            --Increment the index and loop.
SET @iStrike = DATALENGTH(@value) + @iDelimlength
            SET @idx = @idx + 1
            SET @sText = LTrim(Right(@sText,DATALENGTH(@sText) - @iStrike))

            END
        ELSE
            BEGIN
--If you can’t find the delimiter in the text, @sText is the last value in
--@retArray.
 SET @value = @sText
                BEGIN
                INSERT @retArray (idx, value)
                VALUES (@idx, @value)
                END
            --Exit the WHILE loop.
SET @bcontinue = 0
            END
        END
    END
ELSE
    BEGIN
    WHILE @bcontinue=1
        BEGIN
        --If the delimiter is an empty string, check for remaining text
        --instead of a delimiter. Insert the first character into the
        --retArray table. Trim the character from the front of the string.
--Increment the index and loop.
        IF DATALENGTH(@sText)>1
            BEGIN
            SET @value = SUBSTRING(@sText,1,1)
                BEGIN
                INSERT @retArray (idx, value)
                VALUES (@idx, @value)
                END
            SET @idx = @idx+1
            SET @sText = SUBSTRING(@sText,2,DATALENGTH(@sText)-1)

            END
        ELSE
            BEGIN
            --One character remains.
            --Insert the character, and exit the WHILE loop.
            INSERT @retArray (idx, value)
            VALUES (@idx, @sText)
            SET @bcontinue = 0  
            END
    END

END

RETURN
END

答案 1 :(得分:0)

您可以创建存储过程:

CREATE PROC splitstring @ str1 varchar(100),

@ str2 varchar(1),

@laststr varchar(100)OUT

AS

BEGIN

DECLARE @len INT

DECLARE @i INT = 1

SELECT @len=LEN(@str)

WHILE(@i <=@len)

    BEGIN

        if (SUBSTRING(@str1,@i,1)=@str2)

            BEGIN

                SELECT @laststr = SUBSTRING(@str1,@i+1,@len) 
                break
            END
        SELECT @i = @i + 1
    END

END

并使用以下查询

DECLARE @laststr varchar(100) EXEC lastpart @ str1 ='abc / xyz',@ str2 ='/',@ laststr = @ laststr out 选择@laststr