Solved

regexp_substr to get multiple strings from one

Posted on 2013-01-25
7
1,281 Views
Last Modified: 2013-01-25
I've been trying to get both 'string one' and 'string two' returned to me from this query.
Can it be done?
I can get one or the other but not both.
select regexp_substr('string one, string two', '[^,]+',1,1) from dual;
0
Comment
Question by:claghorn
  • 4
  • 2
7 Comments
 
LVL 23

Expert Comment

by:paquicuba
ID: 38819145
Are you trying to get rid of the comma, just replace it:

select replace('string one, string two', ',') from dual;
0
 

Author Comment

by:claghorn
ID: 38819156
no, I want two rows returned.
string one
string two
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 38819173
search EE for str2tbl


select * from table(str2tbl('string one,string two'))
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 200 total points
ID: 38819181
or using built ins

SELECT     REGEXP_SUBSTR(s, '[^,]+', 1, LEVEL)
      FROM (SELECT 'string one, string two' s FROM DUAL)
CONNECT BY LEVEL <= REGEXP_COUNT(s, '[^,]+')


or, if you know you'll always have 2 values

SELECT     REGEXP_SUBSTR('string one, string two', '[^,]+', 1, LEVEL)
      FROM DUAL
CONNECT BY LEVEL <= 2;
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 200 total points
ID: 38819197
another method if you don't have regexp_count but don't know how many elements there will lbe


SELECT     REGEXP_SUBSTR(s, '[^,]+', 1, LEVEL)
      FROM (SELECT 'string one, string two' s FROM DUAL)
CONNECT BY LEVEL <= LENGTH(s) - LENGTH(REPLACE(s, ',')) + 1
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 38819206
you may want to put a trim around the results if you have extra spaces around the comma delimiters


select  TRIM( REGEXP_SUBSTR(s, '[^,]+', 1, LEVEL) ) .....
0
 
LVL 23

Expert Comment

by:paquicuba
ID: 38819225
Just in case what you want are two rows but within the same string, do this:

select regexp_replace('string one, string two',', *',chr(10)) from dual;
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now