select
a.c_code,tb_gds.c_pluno
from
tb_gds(nolock)
left
join
( select substring (c_pluno, 1 , 10 ) as c_code from tb_gds (nolock) where c_pluno like ' % ' )a
on right ( ' 0000000 ' + convert ( varchar ( 20 ), convert ( int , substring (c_pluno, 1 , 10 )) + 1 ), 5 ) = a.c_code
where c_pluno like ' % ' and len (c_pluno) = 5 and c_code is null
( select substring (c_pluno, 1 , 10 ) as c_code from tb_gds (nolock) where c_pluno like ' % ' )a
on right ( ' 0000000 ' + convert ( varchar ( 20 ), convert ( int , substring (c_pluno, 1 , 10 )) + 1 ), 5 ) = a.c_code
where c_pluno like ' % ' and len (c_pluno) = 5 and c_code is null