Similar to this question (Sql Server convert integer to binary string, but I would like to convert a varbinary(16) to its text version instead.
I am doing something tragically wrong as the results of my code will attest.
create function GetGuidBinaryString (@value varbinary(16))
returns varchar(128)
as
begin
declare @vsresult varchar(128)
declare @inti int
select @inti = 128, @vsresult = ''
while @inti>0
begin
select @vsresult=convert(char(1), @value % 2)+@vsresult
select @value = convert(int, (@value / 2)), @inti=@inti-1
end
return @vsresult
end
create table #values (binvalue varchar(128))
delete from #values
declare @intcount int
select @intcount = 0
while @intcount < 100
begin
insert into #values select dbo.GetGuidBinaryString(convert(varbinary(16),convert(bigint,2147483640) + @intcount))
select @intcount = @intcount+1
end
select * from #values
Perhaps there is some implicit conversion I am doing in the function as the function only works correctly for positive integers.