Hello. Try this code. Replace @a with your table name.
declare @b table
(
id int,
newcode int
)
declare @prev int
insert into @b(id,newcode) select id,code from @a
order by id desc
set @prev = 0
update @b
set @prev=ISNULL(newcode,@prev),
newcode=@prev
from @b
select a.id,a.name,b.newcode
from @a a inner join @b b on a.id = b.id