在SQL字符串中获取父路径

时间:2018-06-20 22:28:16

标签: sql sql-server split

我有一堆用斜杠分隔的路径,我需要在这些路径中找到SQL的父级 例如/etc/bin/的父级为/etc/。 (目录的类型为0

/etc/bin/foo.txt的父母是/etc/bin/,依此类推。 (文件的类型为1

我有以下代码,可以正确解析字符串并在字符串为文件时找到父项。

DECLARE @pathsToModify TABLE( path NVARCHAR(MAX), type INT )
INSERT INTO @pathsToModify VALUES('/etc/bin/', 0)
INSERT INTO @pathsToModify VALUES ('/etc/bin/foo.txt', 1)

WHILE EXISTS(SELECT * FROM @pathsToModify)
BEGIN
    DECLARE @path NVARCHAR(MAX)
    SELECT TOP 1 @path = path FROM @pathsToModify
    Declare @products Nvarchar(MAX) = ''
    SET @products = @path
    Declare @individual Nvarchar(MAX) = null
    DECLARE @finalPath NVARCHAR(MAX) = ''

    WHILE LEN(@products) > 0
    BEGIN
        IF PATINDEX('%/%', @products) > 0
        BEGIN
            SET @individual = SUBSTRING(@products, 0, PATINDEX('%/%', @products))
            SET @finalPath = @finalPath + @individual +  '/'

            IF (PATINDEX('%.%',@individual) > 0 OR @products = @individual)
            BEGIN
                RETURN;
            END

            SET @products = SUBSTRING(@products,
                              LEN(@individual + '/') + 1,
                              LEN(@products))
        END
        ELSE
        BEGIN
            SET @individual = @products
            SET @products = NULL
        END
    END

SELECT @finalPath

DELETE FROM @pathsToModify WHERE path = @path
END 

上面的代码正确地找到/etc/bin/foo.txt的父级为/etc/bin/

如何修改此设置,以便为诸如/etc/bin/(即/etc/)之类的目录返回正确的父项?

2 个答案:

答案 0 :(得分:3)

也许您需要这样的东西:

DECLARE @path VARCHAR(MAX)

SET @path = '/etc/bin/sub/foo.txt'
-- Check if the first character is /, if true, then remove it from the path.
SET @path = CASE WHEN CHARINDEX('/', @path) = 1 THEN RIGHT(@path, LEN(@path) - 1) ELSE @path END 

DECLARE 
    @Root       VARCHAR(MAX),
    @fileName   VARCHAR(MAX), 
    @fileRoot   VARCHAR(MAX)

SELECT 
    @Root       = LEFT(@path, CHARINDEX('/', @path) - 1) 
,   @fileName   = RIGHT(@path, CHARINDEX('/', REVERSE(@path)) - 1)
,   @fileRoot   = LEFT(@path, LEN(@path) - LEN(RIGHT(@path, CHARINDEX('/', REVERSE(@path)) - 1)) - 1)




SELECT 
    ROOT                = LEFT(@path, LEN(@Root)) 
,   FileDirectoryRoot   = LEFT(@path, LEN(@path) - (CHARINDEX('/', REVERSE(@fileRoot)) + LEN(@fileName) + 1)    )
,   FileDirectory       = LEFT(@path, LEN(@fileRoot))
,   FileName            = RIGHT(@path, LEN(@fileName)) 

输出:

+------+-------------------+---------------+----------+
| ROOT | FileDirectoryRoot | FileDirectory | FileName |
+-----------------------------------------------------+
| etc  | etc/bin           | etc/bin/sub   | foo.txt  |
+-----------------------------------------------------+

答案 1 :(得分:1)

也许是这样吗?

DECLARE @MyString NVARCHAR(MAX)
SET @MyString = 'Desktop/Subfolder/SubSubFolder/FileName.test'
SELECT LEN(@MyString) - CHARINDEX('/',REVERSE(@MyString)) AS Result

对此概念进行扩展:

DECLARE @MyString NVARCHAR(MAX)
DECLARE @MyString2 NVARCHAR(MAX)

SET @MyString = 'Desktop/Subfolder/SubSubFolder/FileName.test'
SET @MyString2 = 'Desktop/Subfolder/SubSubFolder/'

SELECT LEFT(@MyString, LEN(@MyString) - CHARINDEX('/',REVERSE(LEFT(@MyString, LEN(@MyString) - 1)))) AS Result    
SELECT LEFT(@MyString, LEN(@MyString2) - CHARINDEX('/',REVERSE(LEFT(@MyString2, LEN(@MyString2) - 1)))) AS Result