You apparently want the last element from the string where elements are delimited by a space character.
If you are using Postgres 14, you can use:
split_part('abc de fgh', ' ', -1);
which returns fgh
Or with a column reference: split_part(country, ' ', -1)
In earlier versions, split_part()
doesn't allow negative offsets, so you would need something different:
(string_to_array('abc de fgh', ' '))[cardinality(string_to_array('abc de fgh', ' '))]
It's a bit shorter with a column reference:
(string_to_array(country, ' '))[cardinality(string_to_array(country, ' '))]
This first converts the string into an array, then picks the last element of the array (which in turn is determined using the cardinality()
function that returns the length of the array)