Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Split string into 3 parts in Oracle

Posted on 2011-09-08
7
Medium Priority
?
543 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 2000 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

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. …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
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.

670 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