update cc set country_code = t.country_code from country_codes cc join ( select country_code, country, char_length(trim(cast(country_code as char))) as code_len from country_codes where country_code <> 0 ) t on t.country_code = cast(substr(cast(cc.area_code as char), 1, t.code_len) as signed integer) and cc.country_code = 0 and cc.country like concat(t.country, '%')
I added cc.country like concat(t.country, '%')
to the condition to be more specific, but assumes that each name of the cellular network starts with its own country name, so if it is not, omit it.
Added after comment by @Sachyn:
The test code used in SQLZOO works fine, it is intended only for testing, this is not an update request
select cc.*, t.country_code as new_country_code from ( select 93 as country_code, 93 as area_code , 'Afghanistan' as country union select 0 , 9375 , 'Afghanistan Cellular-AT' union select 0 , 9370 , 'Afghanistan Cellular-AWCC' union select 355, 355 , 'Albania' union select 0 , 35568, 'Albania Cellular-AMC' union select 0 , 35567, 'Albania Cellular-Eagle' union select 213, 213 , 'Algeria' union select 0 , 21377, 'Algeria Cellular-Djezzy' union select 0 , 2135 , 'Algeria Cellular-Wataniya' ) cc join ( select country_code, country, char_length(rtrim(cast(country_code as char))) as code_len from ( select 93 as country_code, 93 as area_code , 'Afghanistan' as country union select 0 , 9375 , 'Afghanistan Cellular-AT' union select 0 , 9370 , 'Afghanistan Cellular-AWCC' union select 355, 355 , 'Albania' union select 0 , 35568, 'Albania Cellular-AMC' union select 0 , 35567, 'Albania Cellular-Eagle' union select 213, 213 , 'Algeria' union select 0 , 21377, 'Algeria Cellular-Djezzy' union select 0 , 2135 , 'Algeria Cellular-Wataniya' ) c where country_code <> 0 ) t on t.country_code = cast(substr(cast(cc.area_code as char), 1, t.code_len) as signed integer) and cc.country_code = 0 and cc.country like concat(t.country, '%')