split out file name from path in postgres

前端 未结 3 397
清歌不尽
清歌不尽 2021-01-17 23:51

I have a field that contains windows file paths, like so:

\\\\fs1\\foo\\bar\\snafu.txt
c:\\this\\is\\why\\i\\drink\\snafu.txt
\\\\fs2\\bippity\\baz.zip
\\\\f         


        
相关标签:
3条回答
  • 2021-01-18 00:29
    CREATE OR REPLACE FUNCTION basename(text) RETURNS text
        AS $basename$
    declare
        FILE_PATH alias for $1;
        ret         text;
    begin
        ret := regexp_replace(FILE_PATH,'^.+[/\\]', '');
        return ret;
    end;
    $basename$ LANGUAGE plpgsql;
    
    0 讨论(0)
  • 2021-01-18 00:31

    You can easily strip the path up to the last directory separator with an expression like

    regexp_replace(path, '^.+[/\\]', '')
    

    This will match the ocassional forward slashes produced by some software as well. Then you just count the remaining file names like

    WITH files AS (
        SELECT regexp_replace(my_path, '^.+[/\\]', '') AS filename
        FROM my_table
    )
    SELECT filename, count(*) AS count
    FROM files
    GROUP BY filename
    HAVING count(*) >= 2;
    
    0 讨论(0)
  • 2021-01-18 00:38
    select regexp_replace(path_field, '.+/', '') from files_table;
    
    0 讨论(0)
提交回复
热议问题