I have a table with these rows as example:
City:
San Francisco (CA) Miami (FL)
As a result I want this:
City:
San Francisco Miami
Any help?
Regards.
You can use REGEX_REPLACE
:
CREATE TABLE tab(City VARCHAR(120));
INSERT INTO tab
VALUES ('San Francisco (CA)');
INSERT INTO tab
VALUES ('Miami (FL)');
SELECT TRIM(REGEXP_REPLACE(city, '\((.+?)\)', '')) AS City
FROM tab;
If you want to UPDATE
you can use:
UPDATE tab
SET City = TRIM(REGEXP_REPLACE(city, '\((.+?)\)', ''))
WHERE INSTR(City, '(') > 0;
SELECT City
FROM tab;
For the sake of argument, here's a REGEXP_SUBSTR( ) example. It assumes you want to keep everything from the start of the strin up to but not including the space before the first open paren.
SQL> with tbl(city) as (
select 'San Francisco (CA)' from dual
union
select 'Miami (FL)' from dual
)
select regexp_substr(city, '^(.*) \(.*$', 1, 1, null, 1) new_city
from tbl;
NEW_CITY
------------------
Miami
San Francisco
SQL>
EDIT: Added a REGEXP_REPLACE solution that does not require a TRIM():
with tbl(city) as (
select 'San Francisco (CA)' from dual
union
select 'Miami (FL)' from dual
)
select regexp_replace(city, '^(.*) \(.*$', '\1') new_city
from tbl;
Or you could do something like
SELECT CASE
WHEN INSTR(CITY, '(') > 2 THEN
SUBSTR(CITY, 1, INSTR(CITY, '(')-2)
ELSE CITY
END AS CITY
FROM CITIES;
Best of luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.