I am using SQL Server in my project, and I have to update the column values of some rows based on other rows in the same table. Here is my table:
| Name | Code | Locale
--------------------
| A    | ab   | en
| A    | cd   | ar
| A    | ef   | ru
| B    | gh   | ar
I need to update Code values of the rows whose Locale is not "en" based the Code value of the row whose Locale is "en" and has the same value in Name. It is guaranteed that each row with "en" in Locale has a unique value in Name. So here is what I hope to achieve
| Name | Code | Locale
--------------------
| A    | ab   | en
| A    | ab   | ar
| A    | ab   | ru
| B    | gh   | ar
I found this thread at SO Update row with data from another row in the same table, and tried the following methods but none of them worked.
UPDATE mytable dt1, mytable dt2 
SET dt1.code = dt2.code 
WHERE dt1.NAME = dt2.NAME 
  AND dt1.code <> 'en' 
  AND dt2.code = 'en'
UPDATE mytable t1 
INNER JOIN mytable t2 ON t1.NAME = t2.NAME and t2.code = 'en' 
SET t1.code = t2.code;
WHERE t1.code <> 'en'
 
     
     
    