Solved

Split string into 3 parts in Oracle

Posted on 2011-09-08
7
509 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 73

Expert Comment

by:sdstuber
Comment Utility
select
regexp_substr(p_username,'[^ ]+',1,1),
regexp_substr(p_username,'[^ ]+',1,2),
regexp_substr(p_username,'[^ ]+',1,3) from dual
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:skahlert2010
Comment Utility
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
Comment Utility
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 73

Expert Comment

by:sdstuber
Comment Utility
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
Comment Utility
Thanks sdstuber!

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

Take care!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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 explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

762 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

6 Experts available now in Live!

Get 1:1 Help Now