Solved

String extraction after a comma and before a middle initial

Posted on 2015-01-28
6
97 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 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

791 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