0

In my table, I have data like PAT5DSA-(ALRP), LAR6DAOP-(RAH) etc..But I want to remove the strings like -(xxxx) or -(xxx) which can be any alphabets inside braces. Tried using the below:

select replace(:code,'-(Aa-Zz)',null) from employee;

But this didn't work..Can anyone please help?

1
  • 1
    Not sure where you got that syntax from (it resembles regular expressions but it isn't). REPLACE does not support anything as fancy. Commented Apr 12, 2019 at 16:47

3 Answers 3

2

We can do a regex replacement using REGEXP_REPLACE:

SELECT REGEXP_REPLACE('PAT5DSA-(ALRP)', '-\(.*?\)', '')
FROM dual;

PAT5DSA
Sign up to request clarification or add additional context in comments.

Comments

1

The plain replace() doesn't understand patterns. You could use a regular expression replace, e.g.:

-- CTE for sample data
with cte (code) as (
  select 'PAT5DSA-(ALRP)' from dual
  union all
  select 'LAR6DAOP-(RAH)' from dual
)
select code, regexp_replace(code, '-\(.*?\)$') as result
from cte;

CODE           RESULT        
-------------- --------------
PAT5DSA-(ALRP) PAT5DSA       
LAR6DAOP-(RAH) LAR6DAOP      

This will remove anything inside a pair of parentheses which is preceded by a dash, at the end of the original string. If the parentheses to be removed could be anywhere in the string then remove the $.

Comments

0

Use INSTR and SUBSTR:

WITH cteVals AS (SELECT 'PAT5DSA-(ALRP)' AS COL_VAL FROM DUAL UNION ALL
                 SELECT 'LAR6DAOP-(RAH)' AS COL_VAL FROM DUAL)
SELECT SUBSTR(COL_VAL, 1, INSTR(COL_VAL, '-')-1)
  FROM cteVals;

Best of luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.