?
Solved

regexp_substr to get multiple strings from one

Posted on 2013-01-25
7
Medium Priority
?
1,400 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 74

Expert Comment

by:sdstuber
ID: 38819173
search EE for str2tbl


select * from table(str2tbl('string one,string two'))
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 800 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 74

Accepted Solution

by:
sdstuber earned 800 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 74

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

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

809 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