Если вам нужны нулевые записи, когда они не найдены:
insert into MyTable (Name, Code1, Code2)
select
Name,
if(not isnull(SomeCode2) and SomeCode2 != ''
and SomeCode2 != 'foo', substring(SomeCode1,1,5), null),
SomeCode2
from SomeTable
;
-или-
Если вы не хотите, чтобы какая-либо строка существовала, когда записи не найдены:
insert into MyTable (Name, Code1, Code2)
select
Name,
substring(SomeCode1, 1, 5),
SomeCode2
from SomeTable
where not isnull(SomeCode2) and SomeCode2 != '' and SomeCode2 != 'foo'
;