2010-09-22 180 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

相關問題