获取“\”之间的子串,其中多个“\”

时间:2013-11-06 17:21:56

标签: sql tsql sql-server-2008-r2 substring trim

找到此解决方案以在slash()字符

之后获取子字符串
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”

2 个答案:

答案 0 :(得分:2)

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

I've gamed SQL Fiddle into showing it working,忽略小提琴中的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 CLR Split组成了正常的.Net Split函数。使用正确的工具,字符串操作更简单,更快捷。


如果需要,here is a NVarChar(MAX) version.

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];

答案 1 :(得分: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'\');