Home > Back-end >  Removing initials Regex 2 spaces
Removing initials Regex 2 spaces

Time:11-11

Hi I am trying to extract the surname from a list of initials and names it is ok for one inital but 2 I just cant seem to get it right

SELECT NAMES, REGEXP_SUBSTR(NAMES,'(\s. $)') FROM PEOPLE

this gets me this top table. I have faffed about online for hours now trying to get to work with some online testers but just seems to break it. anyone help out with the right '(\s. $)' ??

I tried this \s{2,}. $ but no dice comes back with nothing :( same result as below \s.{2,} $

This is what I have

TABLE PEOPLE

Column A Column B
A Peat Peat
D Jones Jones
F Peat Peat
D Jones Jones
F D Peat D Peat
D Jones Jones

What I am after......

TABLE PEOPLE

Column A Column B
A Peat Peat
D Jones Jones
F Peat Peat
D Jones Jones
F D Peat Peat
D Jones Jones

hopefully the question is good enough but this box is not long enough for all my disasters I have tried :) (\s. $)

CodePudding user response:

This worked for me:

 SELECT names
 ,regexp_substr(names, '\\b(\\w )$') last_name
 FROM people;

CodePudding user response:

You can use

SELECT NAMES, REGEXP_SUBSTR(NAMES,'(\S )$') FROM PEOPLE

The \S pattern will match one or more non-whitespace chars and $ will make sure these chars are at the end of string.

  • Related