如何在 Sql 服务器中拆分 Url

How to split Url in Sql server

这里是url

www.abc.com/a/b/c/d/e/f/g/h/i

我想要这样的结果

www.abc.com/a/b/c/d/e/f/g/h/i
www.abc.com/a/b/c/d/e/f/g/h
www.abc.com/a/b/c/d/e/f/g
www.abc.com/a/b/c/d/e/f
www.abc.com/a/b/c/d/e
www.abc.com/a/b/c/d
www.abc.com/a/b/c
www.abc.com/a/b
www.abc.com/a
www.abc.com/

使用While loop。试试这个。

DECLARE @result TABLE(string VARCHAR(500))
DECLARE @str  VARCHAR(500)='www.abc.com/a/b/c/d/e/f/g/h/i',
        @cntr INT=1,
        @len  INT

SET @len = Len(@str)

WHILE @cntr <= @len
  BEGIN
      IF Charindex('/', @str) > 0
        BEGIN
            SELECT @str = LEFT(@str, Len(@str) - 2)
            INSERT INTO @result
            SELECT @str
        END
      ELSE
        BREAK
      SET @cntr+=1
  END

SELECT * FROM @result 

我有另一个解决方案。

declare @S nvarchar(100) = 'www.abc.com/a/b/c/d/e/f/g/h/i'

while PATINDEX('%[/]%' , @S) > 0 BEGIN
    SET @S = LEFT (@S,LEN(@S) - PATINDEX('%[/]%' , REVERSE(@S)))
    SELECT @S
END