Solved

regexp_substr to get multiple strings from one

Posted on 2013-01-25
7
1,312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 74

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 74

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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Password_rules_securitty.. 12 46
Syntax error creating JSON recordset 4 41
How to combine two oracle database together 9 69
Oracle SQL-Queries on a RAC Database 3 39
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

739 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