Solved

Split string into 3 parts in Oracle

Posted on 2011-09-08
7
520 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
[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
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

734 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