Solved

String extraction after a comma and before a middle initial

Posted on 2015-01-28
6
94 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:
SimonAdept 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.
0
 
LVL 48

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Expert Comment

by:SimonAdept
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:SimonAdept
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

747 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

12 Experts available now in Live!

Get 1:1 Help Now