Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Oracle Miscellaneous > Re: regexp_subs...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 6 Topic 6867 of 7280
Post > Topic >>

Re: regexp_substr help, please

by Peter Nilsson <airia@[EMAIL PROTECTED] > Apr 29, 2008 at 08:17 PM

Doug Miller wrote:
> 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),
       '^'
    || '('  -- 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
 




 6 Posts in Topic:
regexp_substr help, please
spambait@[EMAIL PROTECTED  2008-04-29 15:34:24 
Re: regexp_substr help, please
md <mardahl2000@[EMAIL  2008-04-29 09:33:53 
Re: regexp_substr help, please
yf110@[EMAIL PROTECTED]   2008-04-29 09:43:23 
Re: regexp_substr help, please
spambait@[EMAIL PROTECTED  2008-04-29 16:56:40 
Re: regexp_substr help, please
md <mardahl2000@[EMAIL  2008-04-29 09:52:28 
Re: regexp_substr help, please
Peter Nilsson <airia@[  2008-04-29 20:17:57 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 0:26:37 CST 2008.