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

x
?
Solved

Extract initials from string - oracle syntax

Posted on 2010-09-20
11
Medium Priority
?
943 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
[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
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 28

Assisted Solution

by:Naveen Kumar
Naveen Kumar earned 800 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 1200 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

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!

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

664 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