Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I need help parsing a column in SQL Server 2012

Posted on 2014-02-28
4
Medium Priority
?
1,121 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
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.

704 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