Remove all characters after a specific character in PL/SQL
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:
- http://psoug.org/reference/regexp.html
- http://psoug.org/snippet/Regular-Expressions--Regexp-Cheat-Sheet_856.htm
- http://www.regular-expressions.info/oracle.html