2010-09-22 123 views

回答

1

您可以创建在this post中使用的UDF。然后执行:

SELECT CAST(dbo.Hex2Bin('7FE0') as VARBINARY(8000)) AS bin; 
+0

这是我最终使用的。 – 2010-11-26 18:49:15

2

试试这个XML黑客:

declare @hexstring varchar(max); 
set @hexstring = '612E20'; 
select cast('' as xml).value('xs:hexBinary(sql:variable("@hexstring"))', 'varbinary(max)') 
11

如果SQL Server 2008中,您可以通过CONVERT

declare @hexstring varchar(max); 
set @hexstring = 'abcedf012439'; 


/*SQL Server 2005 Specific*/ 
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) 


/*SQL Server 2008 Specific*/ 
set @hexstring = 'abcedf012439'; 
select CONVERT(varbinary(max), @hexstring, 2); 

set @hexstring = '0xabcedf012439'; 
select CONVERT(varbinary(max), @hexstring, 1); 

来源为此直截了当:http://blogs.msdn.com/b/sqltips/archive/2008/07/02/converting-from-hex-string-to-varbinary-and-vice-versa.aspx

相关问题