I need a bit of assistance and sorry if I cannot explain that well. I need the #DATE table updated with values from #COLOR table per the matching START_DATE and END_DATE. The issue is there are gaps in dates and also multiple rows in the #COLOR table that can fulfill the values in the #DATE table. If this happens, I would want to take the later START_DATE from the #COLOR table.
--==== Create some sample data
CREATE TABLE #DATE
(ID VARCHAR(5), BEGIN_DATE VARCHAR(8), END_DATE VARCHAR(8),COLOR_DATE VARCHAR(8),COLOR VARCHAR(10))
CREATE TABLE #COLOR
(ID VARCHAR(5), BEGIN_DATE VARCHAR(8), END_DATE VARCHAR(8),COLOR VARCHAR(10))
INSERT INTO #DATE (ID,BEGIN_DATE,END_DATE,COLOR_DATE,COLOR)
VALUES ('1','20150101','20300101',NULL,NULL)
,('2','20150101','20150101',NULL,NULL)
,('2','20150101','20190131',NULL,NULL)
,('2','20190201','20200101',NULL,NULL)
INSERT INTO #COLOR (ID,BEGIN_DATE,END_DATE,COLOR)
VALUES ('1','20150101','20160101','RED')
,('1','20170101','20180101','BLUE')
,('1','20190101','20240131','ORANGE')
,('2','20150101','20190131','BLUE')
,('2','20190201','20200101','GREEN')
--==== Update does not have the correct value for ID-1, it should be ORANGE with 20190101 COLOR_DATE
--==== I want to take the later date if there is an overlap
UPDATE DATE_TABLE
SET DATE_TABLE.COLOR=COLOR_TABLE.COLOR
,DATE_TABLE.COLOR_DATE=COLOR_TABLE.BEGIN_DATE
FROM #DATE DATE_TABLE
INNER JOIN #COLOR COLOR_TABLE
ON COLOR_TABLE.ID = DATE_TABLE.ID
AND (DATE_TABLE.BEGIN_DATE BETWEEN COLOR_TABLE.BEGIN_DATE AND COLOR_TABLE.END_DATE)
--==== Value for ID-1 should be orange, it should be ORANGE with 20190101 COLOR_DATE
--==== Update did not fulfill ID-2-20150101-20150101 but it did update the other ID-2 fields correctly
UPDATE DATE_TABLE
SET DATE_TABLE.COLOR=COLOR_TABLE.COLOR
,DATE_TABLE.COLOR_DATE=COLOR_TABLE.BEGIN_DATE
FROM #DATE DATE_TABLE
INNER JOIN #COLOR COLOR_TABLE
ON COLOR_TABLE.ID = DATE_TABLE.ID
AND COLOR_TABLE.END_DATE BETWEEN DATE_TABLE.BEGIN_DATE AND ISNULL(DATE_TABLE.END_DATE,'30000101')
SELECT * FROM #COLOR ORDER BY ID,BEGIN_DATE,END_DATE
SELECT * FROM #DATE ORDER BY ID,BEGIN_DATE,END_DATE