We help IT Professionals succeed at work.

Remove the Middle name initials and the suffixes from a string in SQL Server

Cobra967
Cobra967 asked
on
203 Views
Last Modified: 2017-03-26
Hello, I need your help to write a view that will remove the middle name initial and suffix from a single full_name field.

Assumptions:
a) The middle name and suffix can be in the beginning, middle or the end of the string.
b) The middle name will always be a single letter and it may or may not be followed by a period.
c) Once removing the comma and period from the string, anything with less than 2 characters in that string can be assumed to be a middle name or a suffix:

Input data:
John A Doe
A John Doe
John Doe A
John A. Doe
Mr. John Doe A Jr.

Output data:
John Doe
John Doe
John Doe
John Doe

I guess this can be done by
a) Removing period (.) and comma (,)  using for example: REPLACE(REPLACE(full_name, '.', ''), ',', '')  from the string
b) Then, by removing any single word that has a length of 2 or less from the same string.

When I get the corrected code, I will need to update full_name field with the cleaned string.
Thank you for your help!
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you Jester, but it does not appear to work correctly. For example, for:  "Michael S Perdroni Tlaudia T Maers" it returns: "Michael Perdroni Tlaudia" Instead of" Michael "Perdroni Tlaudia Maers". It appears to cut off long strings.

Author

Commented:
I got it. I just had to extend the nvarchar(30) to nvarchar(100);
Thanks Cobra.

I hadn't considered that there might be names that long... what a mouthful!

Noe also that it won't remove the common prefix "Mrs".  It would take a bit more coding to remove prefixes/suffixes longer than 2 characters.  Would likely be easiest to break the string down into individual chunks and compare against known values to exclude.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.