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
 how to join the values in one table

Author  Topic 

sqldev6363
Yak Posting Veteran

54 Posts

Posted - 2011-01-13 : 10:15:28
i have a table result like this

ID Present State
1 y IL
2 y VA
3 NULL NULL
4 y CA
5 NULL NULL
6 NULL NULL

I have second table like this

ID Present State
3 Y TX
5 Y NJ
6 NULL NULL

The thing is i have put the values from secondtable into first table
i need the result like this

ID Present State
1 y IL
2 y VA
3 Y TX
4 y CA
5 Y NJ
6 NULL NULL

Can i get a query for the above logic..i am very much new to this sqlserver..thanks in advance

dev

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-01-13 : 10:22:16
[code]UPDATE t1
SET Present = t2.Present,
State = t2.State
FROM Table1 t1
JOIN Table2 t2
On t1.ID = t2.ID;[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-13 : 10:38:44
if you want only NULL values to be get updated better to add a where condition like below
esp when table2 have more records than in sample which already has values present


WHERE t1.Present IS NULL
AND t1.State IS NULL

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

Go to Top of Page
   

- Advertisement -