select a.c_code,tb_gds.c_pluno from tb_gds(nolock) leftjoin (selectsubstring(c_pluno,1,10) as c_code from tb_gds (nolock) where c_pluno like'%')a onright('0000000'+convert(varchar(20),convert(int,substring(c_pluno,1,10))+1),5)=a.c_code where c_pluno like'%'andlen(c_pluno) =5and c_code isnull