You could just use a simple JOIN to find a match with a LEFT JOIN to eliminate all but the longest match;
UPDATE t1
SET t1.codeA = t2_1.codeB
FROM table1 t1
JOIN table2 t2_1
ON CHARINDEX(t2_1.colB, t1.colA) > 0
LEFT JOIN table2 t2_2
ON CHARINDEX(t2_2.colB, t1.colA) > 0
AND t2_1.codeB <> t2_2.codeB
AND LEN(t2_2.colB) > LEN(t2_1.colB)
WHERE t2_2.colB IS NULL;
Note that it's (probably) not possible to make a CHARINDEX
query like this one (or your original query) use indexes, so the query may be very slow for large amounts of data.
Also, always test first before running SQL updates from random people on the Internet on your production data :)