Abinnaya
Abinnaya

Reputation: 223

How to get the value inside the (brackets) from SQL query

I need a SQL query to get value after brackets..

For example, from the input

Kingdom of Saudi Arabia (KSA) 

I need the output of KSA.

Could anyone help me with this?

Upvotes: 0

Views: 461

Answers (2)

Littlefoot
Littlefoot

Reputation: 142705

Or, using substr + instr combination:

SQL> with test (col) as
  2    (select 'Kingdom of Saudi Arabi ( KSA )' from dual)
  3  select trim(substr(col,
  4                     instr(col, '(') + 1,
  5                     instr(col, ')') - instr(col, '(') - 1
  6                    )) result
  7  from test;

RES
---
KSA

SQL>

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269693

One method uses regexp_replace():

select regexp_replace('Kingdom of Saudi Arabi ( KSA )', '^.*[(](.*)[)].*$', '\1')
from dual;

Upvotes: 2

Related Questions