Getting the last word from a Postgres string, declaratively

If I understand your question correctly you have a string and you’re first splitting it on some separator and then afterwards finding the last element of the array and discarding the rest.

You could miss out the middle man and get the last element directly:

SELECT regexp_replace('foo bar baz', '^.* ', '')

Result:

baz

Leave a Comment