Search code examples
sqloracle-databaseplsqloracle9isubstr

Remove all characters after a specific character in PL/SQL


How do I get a substring from this example value:

 john.abc_1234

I want it to return john.abc.So basically we need to remove all the information after _.

More examples: 1234_abc


Solution

  • You can use SUBSTR and INSTR:

    select substr('john.abc_1234', 1, instr('john.abc_1234', '_') -1)
    from dual
    

    Warning: This is only guaranteed to work if your string actually has an underscore in it

    Update

    Additionally, if you are running from Oracle 10g on, you could take the Regex path, which would more powerfully handle exceptions.

    Here are some links on how to do it in Oracle: