dbTalk Databases Forums  

regexp_substr help, please

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss regexp_substr help, please in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #21  
Old   
Peter Nilsson
 
Posts: n/a

Default Re: regexp_substr help, please - 04-29-2008 , 10:17 PM






Doug Miller wrote:
Quote:
I need to be able to pull just the last name out of a string
consisting of lastname and firstname, separated by a
comma, or space, or comma and space.
Complicating matters somewhat is the fact that lastname
might be something like "Mc Kay" or "St. Louis" so simply
grabbing everything before the first space isn't sufficient.

The closest I've come so far is
select regexp_substr ('St. Louis, Ted', '.{4}[A-Z]+') from dual;
but this returns only
St. L
Start with a 'student' aproach...

with
names as
(
select 'mc winter, first' nme from dual union all
select 'mc. winter, first' nme from dual union all
select 'winter, first second' nme from dual union all
select 'mc winter,first' nme from dual union all
select 'mc. winter,first' nme from dual union all
select 'winter,first' nme from dual union all
select 'mc winter first second' nme from dual union all
select 'mc. winter first' nme from dual union all
select 'winter first' nme from dual union all
select 'macwilliams' nme from dual
)
select
nme,
regexp_replace
(
trim(nme),
'^'
Quote:
| '(' -- with comma
| '([^,]*)' -- surname
| ', *'
| '(.*)' -- given name(s)
| ')'
| '|'
| '(' -- without comma
| '(' -- surname
| '((mc|st)\.?)? *' -- optional prefix
| '[^ ]+'
| ')'
| ' *'
| '(.*)' -- given name(s)
| ')'
| '$',
'"\2\5", "\3\8"', -- "surname", "given"
1, 0, 'i' -- case insensitive
)
from
names

--
Peter


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.