SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple update from another table
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pvong
Yak Posting Veteran

52 Posts

Posted - 04/17/2013 :  12:08:10  Show Profile  Reply with Quote
I know this is simple but I can not figure it out.

Table1----------------Table2
ClientID--Date--------ClientID****Date
1---------null--------1***********1/2/2013
2---------null--------3***********4/15/2013
3---------null

Basically, I just want to update table 1 Date from Table 2 but table 2 does not have all clients.

In my example, only client 1 and 3 will have dates while client 2 will stay as Null.

Thanks!

------------------------------
Using VS2010 / Learning in VB.Net / Win2008 R2 / SQL 2008 R2
Be kind to the newbies because you were once there.

Edited by - pvong on 04/17/2013 12:14:18

James K
Flowing Fount of Yak Knowledge

3761 Posts

Posted - 04/17/2013 :  12:58:02  Show Profile  Reply with Quote
quote:
Originally posted by pvong

I know this is simple but I can not figure it out.

Table1----------------Table2
ClientID--Date--------ClientID****Date
1---------null--------1***********1/2/2013
2---------null--------3***********4/15/2013
3---------null

Basically, I just want to update table 1 Date from Table 2 but table 2 does not have all clients.

In my example, only client 1 and 3 will have dates while client 2 will stay as Null.

Thanks!

------------------------------
Using VS2010 / Learning in VB.Net / Win2008 R2 / SQL 2008 R2
Be kind to the newbies because you were once there.

What do you want to do for client 2? Leave it as null? Also, will there be more than one row in Table2 for a given ClientID? If there is which of the dates do you want to apply to Table1?

Here is one possible way, which assumes that you want to leave client2 date as null, and that there is only one row in Table2 for a given client id.
update t1 set
	[Date] = t2.[Date]
from
	Table1 t1
	inner join Table2 t2 on 
		t1.ClientId = t2.ClientId;
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000