george smiley
george smiley

Reputation: 2791

splitting strings in postgres

I have a string with some spaces in it, and would like to split on the last space, and return the part of the string before that space. Does Postgres support this? I have not been able to solve this with the usual split_part-type functions.
Example : "fort worth tx" -> "fort worth"

Upvotes: 3

Views: 4279

Answers (2)

KARASZI István
KARASZI István

Reputation: 31477

it does not split the source string, but does what you want:

SELECT regexp_replace('fort worth tx', '\\s\\S+$', '');

Upvotes: 3

burkestar
burkestar

Reputation: 777

You'll need to write a plpgsql function to accomplish this. Look here for function to find last occurrence of string.

Upvotes: 0

Related Questions