I have a wierd requirements which I have been struggling for awhile now. here is the code
declare @Value2Convert varchar(max)
declare @temp table( binaryField varbinary(max))
declare @result varchar(max)
select @Value2Convert= 13, @result = ''
select @result= '0'+convert(char(1),@Value2Convert % 2)+@result, @Value2Convert = convert(int, (@Value2Convert / 2))
if @Value2Convert = 0 break
--this return value desired value, I want to save in varbinary field the way it is
select '0x'+ @result
--this will not work as it will return error
--insert into @temp(binaryField)
--this will work but result is incorrect as 0x01010001 will be converted to 0x3031303130303031
insert into @temp(binaryField)
select cast(@result as varbinary(max))
select * from @temp
if the binaryField is varchar type then there wont be a problem, but I was told it must be varbinary. any way to work around this issue?
Thanks in advance!