Skip to Main Content
  • Questions
  • how to extract a part of a string from a string?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, ABBY.

Asked: August 11, 2016 - 7:23 am UTC

Last updated: August 11, 2016 - 9:12 am UTC

Version: 11g database release 2

Viewed 1000+ times

You Asked

how to extract a part of a string from a string without substr and regxp_substr?
like
UNIQUE_ID
-----------------
100A-5567RT-009UY-890IK

WE WANT TO EXTRACT '100A','5567RT'..... THE PART OF THE TEXTS SEPARATED BY '-'.

and Chris said...

Any particular reason you object to using the substr fuctions?

You could XML tokenising operations instead:

with t as (
  select 1 id, '100A-5567RT-009UY-890IK' str from dual
)
select id, subs from t, xmltable(
  'if (contains($X,"-")) then ora:tokenize($X,"\-") else $X'
  passing str as X
  columns subs varchar2(4000) path '.'
);

ID  SUBS    
1   100A    
1   5567RT  
1   009UY   
1   890IK 


HT to Stew Ashton for this method. Surprisingly enough it is faster than substr functions too:

https://stewashton.wordpress.com/2016/08/01/splitting-strings-surprise/
https://stewashton.wordpress.com/2016/08/02/splitting-strings-proof/

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Fast! But...

Robert, August 11, 2016 - 9:55 am UTC

But not as fast as the PL/SQL substr function that Stew displays in the next blog post. :)

<a> https://stewashton.wordpress.com/2016/08/03/splitting-strings-plsql/ </a>

Of course, it does use substr.

More to Explore

Performance

Get all the information about database performance in the Database Performance guide.