How to remove text following a dash in oracle?

10,282

Solution 1

I would just use substr to get everything up to the dash:

select substr(str, 1, instr(str, '-')-1) new_str
from 
(
    select 'ABC-DEF' str
    from dual
)

Solution 2

To do it with a regular expression:

regexp_replace('ABC-DEF', '-.*')

Solution 3

SUBSTR('ABC-DEF', 1, INSTR('ABC-DEF', '-')-1)
Share:
10,282
user560785
Author by

user560785

Updated on June 13, 2022

Comments

  • user560785
    user560785 almost 2 years

    I was looking at

    replace('ABC-DEF', '-') 
    

    and

    regexp_replace('ABC-DEF', '-$') 
    

    to help me remove all characters after a dash but I haven't been able to find a good function or expression that matches what I want to do. What is a good oracle expression that does this?

    ABC-DEF => ABC