31

I need to insert one column's data into another column within the same table.

Can anybody tell me how to write this?

Thanks

4 Answers 4

67
UPDATE table
SET col_2 = col_1
Sign up to request clarification or add additional context in comments.

Comments

16

If you want to copy data from one column to another on the same table:

UPDATE table_name SET
    destination_column_name=orig_column_name
WHERE condition_if_necessary

IF you want to add a new column and copy the original data to that column:

ALTER TABLE table_name
   ADD new_column_name column_type NULL

UPDATE table_name SET
    destination_column_name=orig_column_name
WHERE condition_if_necessary

Comments

2

If you want the column to be non-nullable, then you can set it to a default value before doing the update.

begin transaction
alter table Song add SortArtist nvarchar(128) not null default N''
go
update Song set SortArtist = Artist
commit transaction

Comments

-2

alter table [dbo].[GetPermission]
add username1 varchar(100) ----------------ading new column username1

update GetPermission set username1=username

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.