How can I truncate a prefix string in an Oracle SQL query?

I have a column whose values ​​consist of a prefix and then some value, for example:

ABC-Car
ABC-Dog
ABC-Suit
CBR-Train

In my request, I would like to trim some prefixes, for example. if I trimmed "ABC-", it would give me the following:

Car
Dog
Suit
CBR-Train

The only problem: all characters are valid for the part after the prefix. Perhaps I could:

ABC-ABC-Cat

I want to return this trimmed value:

ABC-Cat

The TRIM and LTRIM functions seem to use pattern / character matching rather than a single line match. Is there a way to accomplish what I want with any of these functions or with another built-in function?

, CASE , , SUBSTR , . , .

+3
1

regexp_replace - , .

:

select regexp_replace('ABC-Car', 'ABC-(.*)','\1') from dual; --returns 'Car'
select regexp_replace('ABC-ABC-Car', 'ABC-(.*)','\1') from dual; --returns 'ABC-Car'
select regexp_replace('ABC-ABC-Car', 'ABC-(.*)','\1') from dual; --returns 'CBR-Train'
+4

Source: https://habr.com/ru/post/1732598/


All Articles