Solved

Extract initials from string - oracle syntax

Posted on 2010-09-20
11
850 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 74

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 74

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 74

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can't Access My Database 57 84
Sybase and replication server 13 56
error doing substr 3 33
Oracle performance tuning 2 15
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to recover a database from a user managed backup

735 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