select a.iCode_ind,a.icode, casewhen b.iCode_ind isnullthen0else1end from TabA a outer apply (selecttop1 iCode_ind from TabA where icode_ind like a.icode_ind+%and icode_ind<>a.icode_ind) b
2.
select*,casewhenexists(select1from tb where iCode_ind<>t.iCode_ind and iCode_ind like t.iCode_ind+%) then1else0end from tb t