Solved

I need help parsing a column in SQL Server 2012

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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
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

679 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