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
 Column Merge

Author  Topic 

saminda
Starting Member

10 Posts

Posted - 2010-02-18 : 19:37:51
I have 2 tables like below, table 1 has one column and table 2 has 3 columns. And I want to extract all the data to the table 1.

table1

column1
111|sa|sasas|good|...|sasasa|......|....|

table2
column1 column2 columns3
111 xxxxxx yyyyyyyy

I need to add column2 & column3 data of table2 to the coloumn1 of table1.
I thought the following code would easily execute this, But its not working at all

update table1 set column1 = column1 + (  SELECT b.column2, b.column3  from table2  b, table1  a WHERE  substring(a.column1,1,3) = b.column1)

I've used first 3 digits of table1 and column1 of table2 as the key(in the where) in order to do the merge.

Appreciate any help on this.
Thanking you
Saminda

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-02-18 : 20:05:45
try . . .

update t1
set column1 = t1.column1 + t2.column2 + t2.column3
from table1 t1
inner join table2 t2 on substring(a.column1,1,3) = t2.column1



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

saminda
Starting Member

10 Posts

Posted - 2010-02-18 : 20:23:12
Thanks a lot khtan,
Works fine now!
Saminda

ras
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-19 : 00:15:39
if any of columns are nullable use:-
update	t1
set column1 = COALESCE(t1.column1,'') + COALESCE(t2.column2,'') + COALESCE(t2.column3,'')
from table1 t1
inner join table2 t2 on substring(a.column1,1,3) = t2.column1


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

Go to Top of Page
   

- Advertisement -