update Exam set PayorPlanKey=T.goodkey from ( select goodvalues.ETLMatchValue as gvaluegtring, goodvalues.Id as goodkey, badvalues.ETLMatchValue as bvaluestring, badvalues.Id as badkey from dim_InsurancePlanName badvalues inner join dim_InsurancePlanName goodvalues on goodvalues.ETLMatchValue = RIGHT(badvalues.ETLMatchValue, LEN(badvalues.ETLMatchValue)-4) where LEN(badvalues.ETLMatchValue) > 4 ) T where PayorPlanKey=T.badkey update dim_InsurancePlanName set ETLOutputKey = goodkey from ( select goodvalues.ETLMatchValue as gvaluegtring, goodvalues.Id as goodkey, badvalues.ETLMatchValue as bvaluestring, badvalues.Id as badkey from dim_InsurancePlanName badvalues inner join dim_InsurancePlanName goodvalues on goodvalues.ETLMatchValue = RIGHT(badvalues.ETLMatchValue, LEN(badvalues.ETLMatchValue)-4) where LEN(badvalues.ETLMatchValue) > 4 ) T where dim_InsurancePlanName.ETLOutputKey=T.badkey