How about this? No dynamic SQL, no (explicit) loop. Just a permanent lookup table.
====================================================================
declare @dec2hex1 table (
id smallint identity(0,1),
hex1 char(1)
)
insert into @dec2hex1 (hex1)
select top 16 null from sysobjects
update @dec2hex1
set hex1 = substring('0123456789abcdef',id+1,1)
create table dec2hex (
dec smallint identity(0,1),
hex char(2)
)
insert into dec2hex (hex)
select a.hex1+b.hex1
from @dec2hex1 a cross join @dec2hex1 b
order by 1
create unique index ix_dec2hex on dec2hex (hex, dec);
create function hex2alpha (
@input varchar(256)
) returns varchar(256)
begin
declare @output varchar(256)
set @output = ''
select @output = @output + char(b.dec)
from dec2hex a, dec2hex b
where b.hex = substring(@input,a.dec*2+1,2)
order by a.dec
return @output
end
select dbo.hex2alpha('416E6E61737461736961')
============================================
Jeff, can you run a performance test for this?