Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace/remove multiple words from string in single statement

I have a table containing full body of water names in one column. ie:

  • Golden Lake
  • Blue Water Lake
  • Muskoka River
  • Sandy Bay

I would like to select the water name, but not the type of water [lake, river, ect...]

Individually, I use:

select replace(watername, 'Lake') from water;

But there are 5 different water types that I was hoping to catch in a single select statement.

like image 880
user1873196 Avatar asked Feb 17 '23 07:02

user1873196


1 Answers

You can use a regex for the replace, using regexp_replace function, for example:

select regexp_replace('Golden Lake, Blue Water Lake, Muskoka River, Sandy B
ay', '( Lake)|( River)|( Bay)', '') from dual

Edit: Since it's 9i, you could try creating a function like this approach.

Or, you may end up doing some weird/ugly select.

like image 198
mrcaramori Avatar answered May 03 '23 22:05

mrcaramori