TSQL/SQL/Access - Splitting the Full Name from the surname

Hi Experts,

I would like to split or remove the Mr., Ms., Ms, Mr, from the front of the FullName?
I have this in the FullName column:
Mr. John Smith
Ms. Jone Smith
Ms John Smith

I would like:
John Smith
Jone Smith
John Smith

In the FullName column

I have this
Select  
    LTRIM(RTRIM(SUBSTRING(FullName, 0, CHARINDEX(' ', FullName)))) As Surname
FROM
  [table]

but all that does is gives me another column with Mr. Ms.
and does not take out the Mr. from FullName

I would like either/or/both in Access query or/both TSQL query

Please help and thanks...
Amour22015Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikas GargAssociate Principal EngineerCommented:
Select  SUBSTRING(FULLNAME,CHARINDEX(' ', @FULLNAME)+1,LEN(FULLNAME)-CHARINDEX(' ', FULLNAME)) FROM tABLE

Open in new window

Amour22015Author Commented:
Hi,

That is looking for a parameter @FullName?

So what do I declare the parameter as?
also will this replace it in the FullName column?

Thanks
PatHartmanCommented:
Are you sure that every name has a prefix?  If a name doesn't have a prefix, you will be stripping off the first name.

Select Mid(FullName, Instr(FullName, " ") + 1) as StrippedName From YourTable
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Kevin CrossChief Technology OfficerCommented:
The first approach likely worked but Expert used @FULLNAME to test in their environment.  You wouldn't need to define that anywhere...just replace references to @FULLNAME with actual column name from your table.  Per other Expert's question on consistency of data and caveat, be very mindful.  For example, I mocked up the below example to check that FULLNAME has two spaces but still is vulnerable when dealing with space in first name (e.g., Mary Jo).
SELECT Surname = CASE WHEN FULLNAME LIKE '% % %' 
                          THEN RIGHT(FULLNAME, LEN(FULLNAME)-CHARINDEX(' ', FULLNAME)) 
						  ELSE FULLNAME END
FROM (
    VALUES('Mr. John Smith'),
		  ('Ms. Jone Smith'),
		  ('Ms John Smith'),
		  ('Joe Smith'),
		  ('Jane'),
		  ('Mary Jo Smith') /* Note unintended consequence. */
) tbl(FULLNAME)
;

/* results:
Surname
-------------------------
John Smith
Jone Smith
John Smith
Joe Smith
Jane
Jo Smith
*/

Open in new window


For what you are doing, it may be better to perform multiple replace statements to explicitly remove the known prefixes...which would get a little more involved and may push you toward a user-defined function.
SELECT Surname = REPLACE(REPLACE(REPLACE(REPLACE(FULLNAME, 'Mr. ', ''), 'Ms. ', ''), 'Ms ', ''), 'Mr ', '')
FROM (
    VALUES('Mr. John Smith'),
		  ('Ms. Jone Smith'),
		  ('Ms John Smith'),
		  ('Joe Smith'),
		  ('Jane'),
		  ('Mary Jo Smith')
) tbl(FULLNAME)
;

/* results:
Surname
-------------------------
John Smith
Jone Smith
John Smith
Joe Smith
Jane
Mary Jo Smith
*/

Open in new window


Not a fan favorite of this either if your data varies a ton (i.e., prefer to clean data and store prefixes in a separate column as well as first and last name) but does the trick if those are the only four titles to deal with.
Amour22015Author Commented:
Kevin Cross,

There is no way of knowing the Mr., Ms.,Mrs., Mis., or even if there is a title in front of the name so matching will not work in this case?

Also what if Mr.John?  without any space that was put incorrectly?

So this is not very easy.  The good news is that I found out that this Mr. John Smith is ok to have in the column and does not need changing.

so this post has turned into a information post for others to see.

Thanks for helping.
PatHartmanCommented:
Hopefully the thread will stand of an example of why fields should be atomic.  Nothing is gained by mushing name parts into a single column and much is lost.
Vikas GargAssociate Principal EngineerCommented:
also will this replace it in the FullName column?

Yes you have to use that column only.

Also what if Mr.John?  without any space that was put incorrectly?


Select  SUBSTRING(FULLNAME,CHARINDEX('.', FULLNAME)+1,LEN(FULLNAME)-CHARINDEX('.', FULLNAME)) FROM TABLE

                                          

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amour22015Author Commented:
Great thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.