Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Copying column within table

Tags:

sql

copying

What is the easiest way to copy the all the values from a column in a table to another column in the same table?

like image 830
Hoopy Frood Avatar asked Oct 06 '08 09:10

Hoopy Frood


People also ask

How do I copy an entire column in SQL?

To copy one column, right-click any cell in the column and click Copy Data » Columns. To copy multiple columns, hold the Ctrl key while clicking cells in the columns, and then right-click and click Copy Data » Columns.

How copy data from table in SQL Server?

Use SQL Server Management StudioIn Object Explorer, right-click Tables and select New Table. In Object Explorer right-click the table you want to copy and select Design. Select the columns in the existing table and, from the Edit menu, select Copy. Switch back to the new table and select the first row.

How do I insert one column data into another column in SQL?

INSERT INTO SELECT Syntax WHERE condition; Copy only some columns from one table into another table: INSERT INTO table2 (column1, column2, column3, ...)

How do you copy values from one column to another?

Select the row or column that you want to move or copy. In the cell, click where you want to paste the characters, or double-click another cell to move or copy the data. or press Ctrl+V. Press ENTER.


2 Answers

With a single statement (if the columns have the same datatype)

UPDATE <tablename>
SET <destination column name> = <source column name>
like image 92
Panagiotis Korros Avatar answered Oct 18 '22 06:10

Panagiotis Korros


This script will update ALL values in the field1 with the values from the field2 in the corresponding row

UPDATE table SET field1 = field2
like image 27
Ilya Kochetov Avatar answered Oct 18 '22 07:10

Ilya Kochetov