SQL server split column

Posted on 2006-05-06
Last Modified: 2008-02-01
In SQL Server:

I've got a FullName column formatted like:
Smith, Tom

I want to split out the values to separate columns:

Question by:webdork
    LVL 75

    Accepted Solution

    UPDATE urTable SET
    FirstName =  SUBSTRING(FullName,1,CHARINDEX(',',FullName)-1),
    LastName =  LTRIM(SUBSTRING(FullName,CHARINDEX(',',FullName)+1,LEN(FullName)))

    Author Comment

    Ok it works... One thing though it places the first name in the LastName column and vis versa. Can you redo with SQL?
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    just interchange the column names''UPDATE urTable SET
    LastName=  SUBSTRING(FullName,1,CHARINDEX(',',FullName)-1),
    FirstName =  LTRIM(SUBSTRING(FullName,CHARINDEX(',',FullName)+1,LEN(FullName)))

    Author Comment

    Thank You

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now