0

找到此解决方案以在斜杠()字符后获取子字符串

DECLARE @st1 varchar(10)
SET @st1 = 'MYTEST\aftercompare'
SELECT @st1
,SUBSTRING(@st1, CHARINDEX('\', @st1) + 1, LEN(@st1))

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/5c3a5e2c-54fc-43dd-b12c-1a1f6784d7d8/tsql-get-substring-after-slash-character

但是有没有办法在第二个斜杠甚至更多之后获取子字符串?

DECLARE @st1 varchar(50)
--Added more slashes
SET @st1 = 'MYTEST\aftercompare\slash2\slash3\slash4'
SELECT @st1
--This part would need some work
--,SUBSTRING(@st1, CHARINDEX('\', @st1) + 1, LEN(@st1))

并且只获取斜杠之间的子字符串。

值:[1] “aftercompare” - [2] “slash2” - [3] “slash3” - [4] “slash4”

4

2 回答 2

2

如果您真的想在 TSQL 中执行此操作,请参见下文。

我已经玩弄了 SQL Fiddle 来展示它的工作原理,忽略小提琴中的 CROSS JOIN,他们只是绕过了 SQLFiddle 对 DECLARE 的限制。

DECLARE @s varchar(8000);
DECLARE @sep char;

SET @s = 'MYTEST\aftercompare\slash2\slash3\slash4';
SET @sep = '\';

WITH [splits] AS (
    SELECT
        0 [index],
        CHARINDEX(@sep, @s) [pos],
        0 [lastPos]
    UNION ALL
    SELECT
        [index] + 1,
        CHARINDEX(@sep, @s, [pos] + 1),
        [pos]
    FROM [splits]
    WHERE
        [pos] > 0)
SELECT
    [index],
    SUBSTRING(
        @s,
        [lastPos] + 1,
        CASE WHEN [pos] = 0
            THEN 8000
            ELSE [pos] - [lastPos] - 1
        END) [value]
FROM [splits];

给出结果

INDEX   VALUE 
0       MYTEST 
1       aftercompare 
2       slash2 
3       slash3 
4       slash4 

在我无法使用表值参数的 SQL 2005 数据库中,我创建了 .Net CLRSplit来组成正常的 .NetSplit函数。使用正确的工具,字符串操作更简单、更快捷。


如果需要,这里有一个NVarChar(MAX)版本。

DECLARE @s nvarchar(max);
DECLARE @sep nchar;

SET @s = N'MYTEST\aftercompare\slash2\slash3\slash4';
SET @sep = N'\';

WITH [splits] AS (
    SELECT
        CAST(0 AS bigint) [index],
        CHARINDEX(@sep, @s) [pos],
        CAST(0 AS bigint) [lastPos]
    UNION ALL
    SELECT
        [index] + 1,
        CHARINDEX(@sep, @s, [pos] + 1),
        [pos]
    FROM [splits]
    WHERE
        [pos] > 0)
SELECT
    [index],
    SUBSTRING(
        @s,
        [lastPos] + 1,
        CASE WHEN [pos] = 0
            THEN 2147483647
            ELSE [pos] - [lastPos] - 1
        END) value
FROM [splits];
于 2013-11-06T17:54:21.143 回答
0

您可以使用此表值函数为每个拆分结果返回一个包含单独行的表:

    ALTER FUNCTION [dbo].[Split]
    (
        @sString nvarchar(2000),
        @cDelimiter nchar(1)
    )
    RETURNS 
    @TblSplits TABLE 
    (
        SplitText nvarchar(2000)
    )
    AS
    BEGIN
        if @sString is null return
        declare @iStart int,
                @iPos int
        if substring( @sString, 1, 1 ) = @cDelimiter 
        begin
            set @iStart = 2
            insert into @TblSplits
            values( null )
        end
        else 
            set @iStart = 1
        while 1=1
        begin
            set @iPos = charindex( @cDelimiter, @sString, @iStart )
            if @iPos = 0
                set @iPos = len( @sString )+1
            if @iPos - @iStart > 0          
                insert into @TblSplits
                values  ( substring( @sString, @iStart, @iPos-@iStart ))
            else
                insert into @TblSplits
                values( null )
            set @iStart = @iPos+1
            if @iStart > len( @sString ) 
                break
        end

        DELETE @TblSplits WHERE SplitText IS NULL;

        RETURN
    END

有了这个,你可以像这样调用函数:

        DECLARE @st1 nvarchar(50)
        --Added more slashes
        SET @st1 = 'MYTEST\aftercompare\slash2\slash3\slash4'
        SELECT @st1

        SELECT * from dbo.Split(@st1,N'\');
于 2013-11-06T18:00:38.743 回答