String extraction after a comma and before a middle initial

Posted on 2015-01-28
Last Modified: 2015-01-28
I am trying to write a substring statement to extract the First name after the last name comma (no space after the comma) and before the space before the middle initial.

Here is an example:


Any suggestions?


Question by:GPSPOW
  • 3
  • 2
LVL 18

Accepted Solution

Simon earned 500 total points
ID: 40576498
declare @str varchar(30) ='MARTIN,DARREN L'
select substring(substring(@str,1,charindex(' ',@str+' ')-1),charindex(',',@str)+1,99)

I've assigned the name to a variable for test purposes, but you could replace @str with your column name.

It works even if the name has no middle initial by padding the string before looking for the space character and stripping off everthing after the first one in the string. It then strips everything up to and including the ',' from the front of the string.
LVL 48

Expert Comment

ID: 40576517
using cross apply allows re-use of aliases, and can help protect against error
    , p1
    , p2
    , case when p2 > p1 then substring(the_field,p1+1, p2-p1) else null end
from (
       select 'MARTIN,DARREN L' as the_field union all
       select 'De Creipny,MARTYN X'
     ) x
cross apply (
  select charindex(',' , the_field), charindex(' ' , the_field)
  ) ca (p1, p2)

Open in new window


Author Closing Comment

ID: 40576522


PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

LVL 18

Expert Comment

ID: 40576524
@PortletPaul: Much more robust. Good example - I hadn't thought about multi-word surnames. I bow to the master :)
LVL 18

Expert Comment

ID: 40576526
@Glen. You might want to review your choice on this one. Paul's solution is much, much better.

Author Comment

ID: 40576533
I appreciate the help.

The first suggestion I worked just as I wanted.


Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now