SQL - string manipulation and copying - SQL Server 2005

Posted on 2011-04-19
Last Modified: 2012-05-11
Hello experts,

I have a table with two columns that I would like to move data from one to the other.  The table looks like:

table: mwc_notes_raw
note_id            note_type
1010 0GEN      null
3013 0GEN      null
4013 0INS        null
1919 0INS        null

I want to split apart t he note_id column and copy everything after the first blank space to the note_type column, and remove everything after the space from the first column, and end up with:

table: mwc_notes_raw
note_id            note_type
1010               0GEN
3013               0GEN
4013               0INS
1919               0INS

Something like:

update mwc_notes_raw
set note_type = RIGHT(Notes_primary_Key,charindex(' ',reverse(Notes_primary_Key))-1),
set Notes_primary_Key =LEFT(Notes_primary_Key,charindex(' ',reverse(Notes_primary_Key))-1)


Question by:robthomas09
    LVL 33

    Assisted Solution

    I recommend doing this in two separate update statements -- first set the value of NOTE_TYPE, then correct the value of NODE_ID.
    LVL 32

    Accepted Solution


    set  Notes_primary_Key = LEFT(note_id, CHARINDEX(' ', note_id)-1),
           note_type  =  RIGHT(note_id, LEN(note_id) -CHARINDEX(' ', note_id))

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    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…

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now