Learned today that converting hexadecimal values to varbinary and vice versa is easier in SQL Server 2008 compared to SQL Server 2005. MS made life simpler with SQL 2K8 :)
See below.
declare @hexstring varchar(max);
set @hexstring = '0xabcedf012439';
select CONVERT(varbinary(max), @hexstring, 1);
set @hexstring = 'abcedf012439';
select CONVERT(varbinary(max), @hexstring, 2);
go
declare @hexbin varbinary(max);
set @hexbin = 0xabcedf012439;
select CONVERT(varchar(max), @hexbin, 1), CONVERT(varchar(max), @hexbin, 2);
go
In SQL Server 2005, we had to use the XQuery functionality.
-- Convert hexstring value in a variable to varbinary:
declare @hexstring varchar(max);
set @hexstring = 'abcedf012439';
select cast('' as xml).value('xs:hexBinary( substring(sql:variable("@hexstring"), sql:column("t.pos")) )', 'varbinary(max)')
from (select case substring(@hexstring, 1, 2) when '0x' then 3 else 0 end) as t(pos)
go
-- Convert binary value in a variable to hexstring:
declare @hexbin varbinary(max);
set @hexbin = 0xabcedf012439;
select '0x' + cast('' as xml).value('xs:hexBinary(sql:variable("@hexbin") )', 'varchar(max)');
go
Other useful links
http://decipherinfosys.wordpress.com/2008/06/11/converting-a-hex-string-to-a-varbinary-or-vice-versa/
http://www.sqlusa.com/bestpractices2005/hexadecimal/
Read More..
 
[7 clicks]