postgresql 如何在select中从完整路径中获取文件名?

2w3rbyxf  于 2023-06-29  发布在  PostgreSQL
关注(0)|答案(2)|浏览(193)

我有一个表A:

Col1   Col2
1     D:\Akagane2\Source\SubModule\ExtractText.vb
2     D:\Akagane2\Source\SubModule\ExtractText.vb

我想选择输出一个表有数据作为

Col1    Col2
1       ExtractText.vb
2       ExtractText.vb

在postgresql中选择,你能帮我吗?

hivapdat

hivapdat1#

就像

SELECT RIGHT('D:\Akagane2\Source\SubModule\ExtractText.vb', POSITION('\' in REVERSE('D:\Akagane2\Source\SubModule\ExtractText.vb')) -1 );
4jb9z9bj

4jb9z9bj2#

在PostgreSQL上。

mole=> CREATE TABLE A (Col1 INTEGER, Col2 VARCHAR);
CREATE TABLE
mole=> INSERT INTO A VALUES (1, 'D:\Akagane2\Source\SubModule\ExtractText.vb');
INSERT 0 1
mole=> INSERT INTO A VALUES (2, 'D:\Akagane2\Source\SubModule\ExtractText.vb');
INSERT 0 1
mole=> INSERT INTO A VALUES (3, '/tmp/Akagane2/Source/SubModule/ExtractText.vb');  -- a unix example
INSERT 0 1
mole=> INSERT INTO A VALUES (4, '/tmp/Akagane2/Source/SubModule/Extract\x20Text.vb'); 
INSERT 0 1
mole=> SELECT * FROM A;
col1 |                    col2                     
------+---------------------------------------------
    1 | D:\Akagane2\Source\SubModule\ExtractText.vb
    2 | D:\Akagane2\Source\SubModule\ExtractText.vb
    3 | /tmp/Akagane2/Source/SubModule/ExtractText.vb
    4 | /tmp/Akagane2/Source/SubModule/Extract\x20Text.vb
(3 rows)
-- Past this point the simple replacements only work if the path
-- delimiter is defined, adjusting dynamically will only be
-- reliable on full paths, not relative ones or mere filenames,
-- leaving us with four-ish options to choose from.

-- option: reliable for DOS paths (1 and 2) only
mole=> SELECT Col1, REGEXP_REPLACE(Col2, '.*\\', '') AS col2 FROM A;
 col1 |                     col2                      
------+-----------------------------------------------
    1 | ExtractText.vb
    2 | ExtractText.vb
    3 | /tmp/Akagane2/Source/SubModule/ExtractText.vb
    4 | x20Text.vb
(4 rows)

-- option: reliable for unix paths (3 and 4) only
mole=> SELECT Col1, REGEXP_REPLACE(Col2, '.*/', '') AS col2 FROM A;
 col1 |                    col2                     
------+---------------------------------------------
    1 | D:\Akagane2\Source\SubModule\ExtractText.vb
    2 | D:\Akagane2\Source\SubModule\ExtractText.vb
    3 | ExtractText.vb
    4 | Extract\x20Text.vb
(4 rows)

-- option: unreliable except for mundane DOS and unix paths.
--   Unix file/dir names CAN contain backslashes, as (4) does:
mole=> SELECT Col1, REGEXP_REPLACE(Col2, '.*(/|\\)', '') AS col2 FROM A;
 col1 |      col2      
------+----------------
    1 | ExtractText.vb
    2 | ExtractText.vb
    3 | ExtractText.vb
    4 | x20Text.vb
(4 rows)

-- option: pick a single replacement based on the first \ or /
--    This can still fail for relative paths, but should be
--    reliable for full paths (only):
mole=> SELECT Col1, CASE WHEN ('/' = SUBSTRING(Col2 FROM 1 FOR 1))
         THEN REGEXP_REPLACE(Col2, '.*/', '')
         ELSE REGEXP_REPLACE(Col2, '.*\\', '')
       END AS col2 FROM A;
 col1 |      col2      
------+----------------
    1 | ExtractText.vb
    2 | ExtractText.vb
    3 | ExtractText.vb
    4 | Extract\x20Text.vb
(4 rows)

相关问题