Help parsing a field into 3 separate fields

Posted on 2005-04-28
Last Modified: 2010-03-19
I have field in a table called Supervisors - Need to parse the data in the field into a new table with 3 separate fields. The data looks like this.. thanks!



Smith, John   11B
Brown, Jane      188
Green,  Bobby    578


LAST        FIRST         NUMBER
Smith       John            11B

Question by:JElster
    LVL 28

    Expert Comment

    Here's how to get the last name and the number:

    SELECT LEFT(Name, CHARINDEX(',', Name) - 1) AS [Last],
                RIGHT(Name, CHARINDEX(' ', REVERSE(Name)) - 1) AS [Number]
    FROM Supervisor

    Will see how to retrieve the first name in a while.
    LVL 1

    Author Comment

    Some NAME fields do not have an ',' or may be null.. then it throws an error.. how can I only process records with ','
    thanks again
    LVL 28

    Accepted Solution

    SELECT LEFT(Name, NULLIF(CHARINDEX(',', Name) - 1, -1)) AS [Last],
                RIGHT(Name, CHARINDEX(' ', REVERSE(Name)) - 1) AS [Number]
    FROM Supervisor

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

    761 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

    9 Experts available now in Live!

    Get 1:1 Help Now