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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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


Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing SCCM 2016 4 48
MAC Dreamweaver connect to external MS SQL Server 2 37
SQL Server Agent "Access Denied" Error 3 33
What does "Between" mean? 6 40
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

737 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