sql

SQL query to copy one column values to another column in same table

UPDATE table_name SET column_name_1 = column_name_2
-- This query will copy column_name_2 values to column_name_1

You can use this query to copy values from one column to another column which exists in the same table. Always use this query carefully because it will change all entries in the column in which we are copying the values of another column. You can use the where clause with this to change values based on condition.

For example, If there is a table named as 'Employee' that has two columns FirstName and UserName and we want to copy FirstName values to the UserName column.

To insert all entries of the FirstName column to the UserName column use the below SQL query.

UPDATE Employee SET UserName = FirstName

This will update the Employee table as below

Was this helpful?