Solved

Extract initials from string - oracle syntax

Posted on 2010-09-20
11
816 Views
Last Modified: 2012-05-10
Hello
Have a string field which may contain one or more names seperated by a comma. I need to return just the Initials.
eg
Field1                                                            Field2(Initials)
Fred Bloggs, Dave Smith, Eileen Bab            FB, DS, EB
John Sayer                                                   JS
etc etc
0
Comment
Question by:philsivyer
  • 5
  • 3
  • 3
11 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 33714949
what is the output you need for that data intput ?
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 33714961
you mean field2 is the output you wanted ? do you need it as single sql query or pl/sql code block is also ok for you ?
0
 

Author Comment

by:philsivyer
ID: 33714980
Field 2 is the required output and preferably as single sql query
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.

 
LVL 28

Assisted Solution

by:Naveen Kumar
Naveen Kumar earned 200 total points
ID: 33715232
try this :
create or replace function f1(var varchar2) return varchar2
is
rr varchar2(100) :=null;
begin
rr:= substr(var,1,1);
for x in 1..length(var) loop
if ( substr(var,x,1) = ' ') then
rr := rr || substr(var,x+1,1);
elsif ( substr(var,x,1) = ',') then
rr := rr || substr(var,x,1);
end if;
end loop;
return rr;
end;
/

select name1, f1(name1) from test1;

This works for me as you wanted.

But this can also be done with single sql with or without regular expressions.  

Thanks
0
 

Author Comment

by:philsivyer
ID: 33715670
I'd be intrigued with single sql solution
Regards
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 33715677
10g and above, try this...

regexp_replace(field1,'([A-Z])[a-z ]*','\1')
0
 

Author Comment

by:philsivyer
ID: 33716990
The regex looks interesting- what if in the string we also have something like ....

Field 1                                                                                      Required Output
Fred Bloggs, Dave Smith (Instructor), Eileen Bab                    FB, DS, EB
Phil Smith and Alan Vine, John Brown                                      PS , AV, JB


So, I would like to leave out "and" and anything in brackets.

Regards
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 33717080
you'll have to nest replacements


 REGEXP_REPLACE(
           REGEXP_REPLACE(REPLACE(field1, ' and ', ', '), '\(.*\)'),
           '([A-Z])[a-z ]*',
           '\1'
       )
0
 

Author Comment

by:philsivyer
ID: 33717160
This looks good - one small problem is that if I get more than one set of brackets it leaves out
other required data - eg

Fred Bloggs, Dave Smith (Instructor), Eileen Bab (Head of Dept) it returns FB, DS.

Any good sites re regex and oracle?  
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 300 total points
ID: 33717351
oops  try this instead

       REGEXP_REPLACE(
           REGEXP_REPLACE(REPLACE(field1, ' and ', ', '), '\([^)]*\)'),
           '([A-Z])[a-z ]*',
           '\1'
       )

I use Appendix C in the SQL Reference

http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/ap_posix.htm#i691974

in 11g the SQL Reference moves the Regular Expressions to Appendix D
0
 

Author Comment

by:philsivyer
ID: 33717382
Thanks - really good
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

786 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