Solved

I need help parsing a column in SQL Server 2012

Posted on 2014-02-28
4
1,098 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
[X]
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
4 Comments
 
LVL 40

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

636 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