In SQL server 2008:
Suppose I have two tables.
Table1 has 3 fields: Name, Date1 and Date2. Currently, all the Date2 entries are NULL. (Name, Date1) form a unique key.
Table2 has 2 fields: Name and Date2. (Name, Date2) form a unique key.
Every "Name" in Table1 has at least one corresponding entry in Table2.
Now, I want to update all the Date2 entries in Table1 (remember they are all NULL right now) to the Date2 entry in Table2 that is the closest to Date1 in Table1. I.e. the date that would give the result of:
min(datediff(dd,Table1.Date1,Table2.Date2))
So to be clear, if I have the following entries:
Table1:
[Name]: Karl, [Date1]: 1/1/2009, [Date2]: NULL
Table2:
[Name]: Karl, [Date2]: 1/1/2000
[Name]: Karl, [Date2]: 1/7/2009
[Name]: Karl, [Date2]: 1/1/2010
Then I want to update Table1.Date2 to '1/7/2009' since that is the closest date to '1/1/2009'.
Thanks a lot
Karl