Get string after character '/'

I want to extract the line after the '/' character in a SELECT query for PostgreSQL.

Field source_pathname, table name movies_history.

Data examples:

Values ​​for source_path:

  • 184738 / file1.mov
  • 194839 / file2.mov
  • 183940 / file3.mxf
  • 118942 / file4.mp4

Etc. All values ​​for source_path are in this format.

  • random_number / filename.xxx

I need to get only the string "file.xxx".

+5
source share
2 answers

If your case is so simple: exactly one /per line, use : split_part()

SELECT split_part(source_path, '/', 2) ...

/, , reverse(), reverse() 2- :

SELECT reverse(split_part(reverse(source_path), '/', 1)) ...

( ) substring() :

SELECT substring(source_path, '[^/]*$') ...

:

[...].. .
[^...].. ^ ( ).
*.. 0- .
$.. .

SQL Fiddle demo

+18

SQL FIDDLE

SELECT substring('1245487/filename.mov' from '%/#"%#"%' for '#');

:

%/

% , a /

#"%#"

# , for '#', "

, <placeholder> % <placeholder>, , . % /

:

 SELECT substring(source_path from '%/#"%#"%' for '#');
 FROM movies_history
+1

Source: https://habr.com/ru/post/1605798/


All Articles