How do I duplicate a column in a SQL Table?
14,436
Solution 1
A combination of Add a column with a default value to an existing table in SQL Server and How can I copy data from one column to another in the same table? woold look something like:
ALTER TABLE Table1
ADD SubCategory2 {Type of subcategory 1} {NULL|NOT NULL}
UPDATE Table1 SET SubCategory2 = SubCategory;
Solution 2
select * from Table1
---------Query-----------
select * , SubCategory as SubCategory2 FROM Table1
----------------------
Related videos on Youtube
Author by
user3115933
Updated on August 18, 2022Comments
-
user3115933 over 1 year
I am using
SQL Server 2014
and I have atable
namedTable1
. Assuming I want to duplicate a column namedSubCategory
into a new column calledSubCategory2
(with the same values as in columnSubCategory
), what would be theSQL
syntax
to do this?Note: Idea here is to keep column
Subcategory
but create another columnSubCategory2
and modify some specific values in that new column.Example (assuming Table1 has only 2 columns):
Serial Subcategory 1 UK 2 UK 3 FR 4 DE
Expected output:
Serial Subcategory Subcategory2 1 UK UK 2 UK UK 3 FR FR 4 DE DE
-
Tsahi Asher about 5 yearsThis will add new rows, and not update existing rows.
-
Eugene Ogongo about 5 yearsCan you check example and expected out of the question. I think I have answered it as per the example is given.