Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert one column into other column within the same table in SQL Server

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

Can anybody tell me how to write this?

Thanks

like image 520
kumar Avatar asked Apr 04 '11 17:04

kumar


People also ask

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

Click the tab for the table with the columns you want to copy and select those columns. From the Edit menu, click Copy. Click the tab for the table into which you want to copy the columns. Select the column you want to follow the inserted columns and, from the Edit menu, click Paste.

How can we UPDATE one column value with another column in the same table?

In such a case, you can use the following UPDATE statement syntax to update column from one table, based on value of another table. UPDATE first_table, second_table SET first_table. column1 = second_table. column2 WHERE first_table.id = second_table.

Can we add a column between two columns in SQL Server?

In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table.

How do I combine two columns in SQL?

SELECT *, CONCAT(FIRSTNAME, LASTNAME) AS FIRSTNAME FROM demo_table; Output: Here, we can see that FIRSTNAME and LASTNAME is concatenated but there is no space between them, If you want to add space between the FIRSTNAME and LASTNAME then add space(' ') in CONCAT() function. This method will change the original table.


2 Answers

UPDATE table SET col_2 = col_1 
like image 143
Cade Roux Avatar answered Oct 05 '22 20:10

Cade Roux


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 
like image 30
Doliveras Avatar answered Oct 05 '22 18:10

Doliveras