Rudy Herdez Rudy Herdez - 1 year ago 39
SQL Question

Get string after '/' character

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

The field name is

, table name is

Data Examples:

Values for source_path:

  • 184738/

  • 194839/

  • 183940/file3.mxf

  • 118942/file4.mp4

And so forth. All the values for source_path are in this format

  • random_number/

I need to get '' string only.

Answer Source

If your case is that simple: exactly one / in the string, use split_part():

SELECT split_part(source_path, '/', 2) FROM movies_history;

If there can be multiple /, and you want the string after the last one, use substring() with a regular expression:

SELECT substring(source_path, '[^/]*$') FROM movies_history;


[...] .. encloses a list of characters to form a character class.
[^...] .. if the list starts with ^ it's the inversion (all characters not in the list).
* .. quantifier for 0-n times.
$ .. anchor to end of string.