I have a case I need to replace multiple substrings within 1 column using PostgreSQL.
Example:
Region | City |
---|---|
Moscow obl | Istra |
Rep Bashkortostan | Ufa |
Udmurtskaya Rep | Izhevsk |
I need all substrings ‘obl’ to be replaced with ‘oblast’ and ‘Rep’ replaced with ‘Republic’
I tried to use subquery like
select regexp_replace
((select regexp_replace(
wt.region_with_type,
'sobl$','oblast')
from work_table wt),
'^Reps|sRep$','Republic') as project_f_region
from work_table wt
But it throws an error
postgresql error: more than one row returned by a subquery used as an expression
I tried to google the answer, but couldn’t find any solutions.
I know I can use CTE, but as for me the query gets too bulky
Is there any other way to use multiple replaces with different substrings?
3
Answers
I suggest using CASE.
You can use multiple
regex_replace
functions :You can nest function calls directly, without mixing in subqueries. Simply
in contrast to your
which would only work if you had a single row in your
work_table
. I’m wondering if it wasn’t just a typo, a lost or misplaced closing parenthesis)
and you actually triedOr, in full:
Which technically uses an entirely correct scalar subquery, but that is just syntax noise you don’t really need. It would have the exact same result as