Solved

I need help parsing a column in SQL Server 2012

Posted on 2014-02-28
4
1,049 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

920 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

16 Experts available now in Live!

Get 1:1 Help Now