Monday, August 4, 2008

Oracle: Regular Expression: Examples

How to split strings like "ABC Company (XXX)" into "ABC Company", "XXX"


Select col1, regexp_substr(col1,'[^(]*') Comp_Name,
ltrim(regexp_substr(col1,'\([^)]*'),'(') comp_code
from ( Select 'ABC Company (XXX)' col1 from dual ).

Script does 3 things

1) For name, It takes everything till it hits '('
2) For Code, it takes everything from '(' [you need to escape open paranthesis symbol], till it finds the closing parenthesis ')'.
3) Ltrim is to remove the Open parenthesis in the code.


No comments:

Post a Comment