Remove all characters after a specific character in PL/SQL

sandy picture sandy · Oct 10, 2011 · Viewed 56k times · Source

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

Answer

Adriano Carneiro picture Adriano Carneiro · Oct 10, 2011

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: