Solved

I need help parsing a column in SQL Server 2012

Posted on 2014-02-28
4
1,033 Views
Last Modified: 2014-04-14
Hi Experts,
I have a question regarding the parsing of a column in my Employee table.  The NAME column stores the data as follows:
DOE, JON Y

I want to parse the data so that it puts the data in 3 separate columns:
LastName      FirstName      MiddleInitial
DOE            JON            Y


How can I do this?  What syntax do I use?


Thanks in advance,
mrotor
0
Comment
Question by:mainrotor
4 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 39895693
select
      left('DOE, JON Y', charindex(',','DOE, JON Y')-1) as FirstName,
      --LastName
      SUBSTRING(
      --select
            SUBSTRING('DOE, JON Y',
            CHARINDEX(', ','DOE, JON Y')+2,
            len('DOE, JON Y') - CHARINDEX(', ','DOE, JON Y')+2)
            ,1,
      --select                              
            LEN (
            SUBSTRING('DOE, JON Y',
            CHARINDEX(', ','DOE, JON Y')+2,
            len('DOE, JON Y') - CHARINDEX(', ','DOE, JON Y')+2)
                  )-2
            ) as LastName,
      --Initial
      SUBSTRING(
      --select
            SUBSTRING('DOE, JON Y',
            CHARINDEX(', ','DOE, JON Y')+2,
            len('DOE, JON Y') - CHARINDEX(', ','DOE, JON Y')+2)
            ,CHARINDEX (' ',SUBSTRING('DOE, JON Y',
                                    CHARINDEX(', ','DOE, JON Y')+2,
                                    len('DOE, JON Y') - CHARINDEX(', ','DOE, JON Y')+2
                              ))+1            
            ,1) as Initial            
--all you need now is to replace in the above 'DOE, JON Y' with the table column where the data is and add a FROM to the above statement.
0
 
LVL 4

Accepted Solution

by:
rshq earned 500 total points
ID: 39896955
Hi
 Please test this code
SELECT        SUBSTRING([NAME], 1, CHARINDEX(',', [NAME]) - 1) AS LastName , SUBSTRING([NAME], CHARINDEX(',', [NAME]) + 1, CHARINDEX(' ', [NAME]) 
                         - CHARINDEX(',',[NAME])) AS FirstName , SUBSTRING([NAME]', CHARINDEX(' ', [NAME]) + 1, LEN([NAME]) - CHARINDEX(' ', [NAME])) AS MiddleInitial

Open in new window

0
 
LVL 15

Expert Comment

by:JimFive
ID: 39901605
Don't forget to consider names without middle initials, names with middle names instead of initials, names with suffixes:  DOE JR., JON Y    or DOE, JON Y JR.
Names with multiple middle initials.  Etc.

There is not a good and happy universal solution to this problem.
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39901634
Hi,

From a long and distant past I have a copy of PC Techniques that did all this in C.

It did almost everything but went the other way. ie
william gates iii

to
Gates, William III

The only ones it didn't capitalize correctly were MacD ...

Regards
  David
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

705 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

13 Experts available now in Live!

Get 1:1 Help Now