Copy data from one column to other column (which is in a different table)
Solution 1:
Here the query:
Same Table:
UPDATE table_name
SET column1 = column2
Different Table:
UPDATE table_name1
SET column1 = (
SELECT column2
FROM table_name2
WHERE table_name1.id = table_name2.id
);
Solution 2:
In SQL Server 2008 you can use a multi-table update as follows:
UPDATE tblindiantime
SET tblindiantime.CountryName = contacts.BusinessCountry
FROM tblindiantime
JOIN contacts
ON -- join condition here
You need a join condition to specify which row should be updated.
If the target table is currently empty then you should use an INSERT instead:
INSERT INTO tblindiantime (CountryName)
SELECT BusinessCountry FROM contacts
Solution 3:
Table2.Column2 => Table1.Column1
I realize this question is old but the accepted answer did not work for me. For future googlers, this is what worked for me:
UPDATE table1
SET column1 = (
SELECT column2
FROM table2
WHERE table2.id = table1.id
);
Whereby:
- table1 = table that has the column that needs to be updated
- table2 = table that has the column with the data
- column1 = blank column that needs the data from column2 (this is in table1)
- column2 = column that has the data (that is in table2)
Solution 4:
Hope you have key field is two tables.
UPDATE tblindiantime t
SET CountryName = (SELECT c.BusinessCountry
FROM contacts c WHERE c.Key = t.Key
)