Search code examples
sqlsubstrself-join

How to store part of a field value in another column in SQL


So I'm trying to get a part of a value from a column and insert that part into another column - new column. BOTH columns are in the same table. So what i want should look something like this:

id      newColumn      oldColumn
1       12             123 some text
2       24             246 some text
....

I know how to get 12 and 24 using SUBSTR, but how do i enter the data for each row in the table. Should i be using self-join or something else?


Solution

  • First you have to add new col using following command:-

    ALTER TABLE TAB_NAME
    ADD COLUMN COL_NAME(VARCHAR(10));
    

    After that execute this command:-

    UPDAET TAB_NAME
    SET COL_NAME = SUBSTRING(OLDCOLUMN, 1, 2);
    

    I think this might help you.