Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

String extraction after a comma and before a middle initial

Posted on 2015-01-28
6
Medium Priority
?
105 Views
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:

MARTIN,DARREN L

Any suggestions?

Thanks

Glen
0
Comment
Question by:GPSPOW
  • 3
  • 2
6 Comments
 
LVL 18

Accepted Solution

by:
Simon earned 2000 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.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40576517
using cross apply allows re-use of aliases, and can help protect against error
select
      the_field
    , 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

0
 

Author Closing Comment

by:GPSPOW
ID: 40576522
Perfect

Thanks

Glen
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 18

Expert Comment

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

Expert Comment

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

Author Comment

by:GPSPOW
ID: 40576533
I appreciate the help.

The first suggestion I worked just as I wanted.

Glen
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

877 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