Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 updating a coulmn with data from 2 tables

Author  Topic 

scottdg
Starting Member

20 Posts

Posted - 2012-09-10 : 15:03:05
I created a column in a table and now I want to update it with concatenated data from an existing column in the same table and a column from another table:

table A
Full_Name_And Company (New Column)
Full_Name

Table B
Company

Given the 2 tables above how do I update Full_Name_And Company with a concatenation of Full_Name + Company?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-10 : 15:05:00
how are two tables related? is there a common field?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

scottdg
Starting Member

20 Posts

Posted - 2012-09-10 : 15:22:35
Yes sorry...they have a common ID#

table A
Full_Name_And Company (New Column)
Full_Name
Customer_ID

Table B
Company
Customer_ID
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-10 : 15:32:05
[code]
UPDATE a
SET a.Full_Name_And Company = COALESCE(a.Full_Name,'') + COALESCE(b.Company ,'')
FROM TableA a
INNER JOIN TableB b
ON b.Customer_ID = a.Customer_ID
[/code]

if columns are NOT NULL you can avoid the COALESCE()

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

scottdg
Starting Member

20 Posts

Posted - 2012-09-10 : 15:46:03
Hmm...looks very similar to what I tried without the COALESCE() which I will need for Company_Name. I will try it again.

Thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-10 : 15:52:39
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -