sameer
sameer

Reputation: 41

How to copy one column data into another column in oracle

I want to copy one column data into another column without replacing the old data. For example :

Table-1

Column1    Column2 

SONY       Sony  Desc
Lenovo     Lenovo Desc 
Nokia      Nokia Desc 

I would like result like

Column 1        column2
SONY            Sony  Desc
Sony  Desc
Lenovo          Lenovo Desc 
Lenovo Desc      
Nokia            Nokia Desc 
Nokia Desc

I have tried my query not match

Update table1 set column1 = column2

Upvotes: 2

Views: 9672

Answers (2)

Mick Mnemonic
Mick Mnemonic

Reputation: 7956

If you want to add rows, you need to INSERT instead of UPDATE. This statement will add new rows that have the column1 value copied from column2 of another row in the table and the primary key (export_config_id) value taken from a sequence (seq_export_config_id):

INSERT INTO table1
(export_config_id, column1)
SELECT seq_export_config_id.NEXTVAL, column2 FROM table1;

Upvotes: 2

Lalit Kumar B
Lalit Kumar B

Reputation: 49062

If column1 has a NOT NULL constraint, or if it is has the primary key constraint, then you won't be able to insert NULL values. You need to filter out the NULL values:

INSERT INTO table1 (column1)
  SELECT column2 
   FROM table1 
  WHERE column2 IS NOT NULL;

Upvotes: 1

Related Questions