I have used order by Len(name) desc so that AA will come first, but since order by will apply atlast, it will not work.
One option I have in mind is to use a subquery like
Declare @tab Table(name varchar(10), value varchar(10))
Declare @mystring varchar(10)
set @mystring='AA'
insert into @tab
select * from (select 'A' Name , 1 Value
union
select 'AA',2) A order by LEN(name) desc
select @mystring=REPLACE(@mystring,name,value) from @tab
select @mystring
What you think?