Solved

Split string into 3 parts in Oracle

Posted on 2011-09-08
7
515 Views
Last Modified: 2012-05-12
Hello experts!

I need to split one string into 3 parts i.e. firstname, surname and degree.

While I am able to split into first and surname, I cannot extract the degree if there's one mentioned.

The strings to be split apart come in the following way:

Dr. James Example

Could you please assist me in splitting this string apart into three parts?

A million thanks in advance! I am curious about your appraches!


My current code is attached below. p_username is a parameter passed to the procedure!

select secondname || '\, ' || firstname || ' (LWF)' into ad_name from ( 
select substr(replace(p_username,',',''), 1, (instr (replace(p_username,',',''), ' ') - 1)) as firstname, 
substr (replace(p_username,',',''), (instr (replace(p_username,',',''), ' ',1,1) + 1), length (replace(p_username,',',''))) as secondname
from dual);

Open in new window

0
Comment
Question by:skahlert2010
  • 4
  • 3
7 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 36502785
select
regexp_substr(p_username,'[^ ]+',1,1),
regexp_substr(p_username,'[^ ]+',1,2),
regexp_substr(p_username,'[^ ]+',1,3) from dual
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 36502816
if this is going to be used in pl/sql  don't use   select from dual

simply do direct pl/sql assignment


v_degree := regexp_substr(p_username,'[^ ]+',1,1);
v_firstname := regexp_substr(p_username,'[^ ]+',1,2);
v_surname := regexp_substr(p_username,'[^ ]+',1,3);



also,  your original query looks like it's searching for commas, but your example input doesn't contain any
what's the intent there?
0
 

Author Closing Comment

by:skahlert2010
ID: 36502844
Wow you're ingenious and very firm in regexp functions as I can tell by now.

Thanks a lot sdstuber! That was very quick!
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:skahlert2010
ID: 36502881
Well, would it be possible to have a fixed assignment for degree, first and surname?

For example If the person doesn't have a degree then
"regexp_substr(:p_username,'[^ ]+',1,1)  title" holds the firstname while the alias firstname contains the lastname. Lastname in turn is null.



0
 

Author Comment

by:skahlert2010
ID: 36502925
Okay, this is my solution to the problem I asked above! I know it's not perfect but it works:
select 
case when title not in ('Dr.', 'Prof.') then title else firstname end as firstname,
case when title not in ('Dr.', 'Prof.') then firstname else secondname end as secondname,
case when title in ('Dr.', 'Prof.') then title end as title
from (select
regexp_substr(:p_username,'[^ ]+',1,1) title,
regexp_substr(:p_username,'[^ ]+',1,2) firstname,
regexp_substr(:p_username,'[^ ]+',1,3) secondname from dual)

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36503361
that should work,
I'd have done it something like this...
check to see if there were 3 names, if so use them all, if not, skip the title


SELECT NVL2(s3, s1, NULL) title, NVL2(s3, s2, s1) firstname, NVL(s3, s2) surname
  FROM (SELECT REGEXP_SUBSTR(:p_username, '[^ ]+', 1, 1) s1,
               REGEXP_SUBSTR(:p_username, '[^ ]+', 1, 2) s2,
               REGEXP_SUBSTR(:p_username, '[^ ]+', 1, 3) s3
             from dual)
0
 

Author Comment

by:skahlert2010
ID: 36504192
Thanks sdstuber!

Nice solution! I'll adopt it! Sometimes it's amazing how many ways lead to Rome!

Take care!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
error in my cursor 5 41
Email query results in HTML 6 31
awk and Pythagoras? 5 19
TSQL Challenge... 7 37
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

828 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