vivek
vivek

Reputation: 111

Get the string before a specific character

i have a query which return the target name in the below format: Database_hostname_instance I need to extract the below part only from this: Database_hostname For this i tried to use the below ,but it gives the string before the initial '' like 'Database regexp_substr(target_name, '^[^]+')

Can you please suggest how to get the string before the 1st occurance of '_' from the end.Thanks.

Upvotes: 0

Views: 68

Answers (2)

Jim Macaulay
Jim Macaulay

Reputation: 5141

You can also use INSTR function

select 'Database_hostname_instance', substr('Database_hostname_instance', 0, 
instr('Database_hostname_instance', '_', -1)-1)
from dual;

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269513

If there are always two underscores, you can use:

select regexp_replace('Database_hostname_instance', '_[^_]*$', '')
from dual

This lops off the last underscore and everything after it.

Upvotes: 2

Related Questions