SQLTeam.com | Weblogs | Forums

Updating one table from another table based on date range and id


#1

How to i update table 2 based on the condition from table 1
condition is to update table two based on id and date range

table 1

ID Description Datefrom DateTo

1 abc 2016-01-01 2016-01-04
2 xyz 2016-02-01 2016-02-04

Table 2

ID Date Description

1 2016-01-01 abc
1 2016-01-02 abc
1 2016-01-03 abc
1 2016-01-04 abc
2 2016-02-01 xyz
2 2016-02-02 xyz
2 2016-02-03 xyz
2 2016-02-04 xyz


#2
UPDATE T2
SET
    T2.[Description] = T1.[Description]
FROM
    Table2 AS T2
    INNER JOIN Table1 AS T1
        ON T2.ID = T1.ID
        AND T2.[Date]>= T1.DateFrom
        AND T2.[Date]<= T1.DateTo

#3

thanks a lot. Let me try.