4

A column in my table in Postgres has varchar values in the format: 'str1/str2' or 'str1/str2/str3' where str represents any string.

I want to write a select query which will return me str2. I surfed but couldn't find any proper function.

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Jayant
  • 323
  • 3
  • 13

1 Answers1

8

Use split_part():

SELECT split_part(col, '/', 2) AS result
FROM tbl;

As Victoria pointed out, the index is 1-based,

Obviously, the delimiter needs to be unambiguous. It (/ in your example) cannot cannot be part of a substring. (Unless that's to the right of what you extract, which is ignored anyway.)

Related:

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228