Remove all characters after a specific character in PL/SQL

女生的网名这么多〃 提交于 2019-12-04 07:54:00

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:

You can use this monstrosity, when you're not sure if all your values contain the character you want to cut off your string at.

SELECT
    (CASE WHEN INSTR(field, '_') > 0
        THEN substr(field, 1, instr(field, '_') -1)
        ELSE field
    END) AS field
FROM
    dual
kmeow1024

This guy figured it out! http://programcsharp.com/blog/post/strip-non-numeric-characters-from-a-string-in-sql-server

SELECT
    (SELECT CAST(CAST((
        SELECT SUBSTRING(FieldToStrip, Number, 1)
        FROM master..spt_values
        WHERE Type='p' AND Number <= LEN(FieldToStrip) AND
            SUBSTRING(FieldToStrip, Number, 1) LIKE '[0-9]' FOR XML Path(''))
    AS xml) AS varchar(MAX)))
FROM
    SourceTable
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!