SQL Server hex string to varbinary conversion

在 MS SQL Server 裡做 hex string 與 binary 的轉換範例如下:

From MSDN

In SQL Server 2008, these conversions are even more easier since we added support directly in the CONVERT built-in function. The code samples below show how to perform the conversion(s):

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

Ex:

select convert(varchar(max), HASHBYTES('md5', '[email protected]'), 1);
0x5F4DCC3B5AA765D61D8327DEB882CF99
select convert(varchar(max), HASHBYTES('md5', '[email protected]'), 2);
5F4DCC3B5AA765D61D8327DEB882CF99
select convert(varbinary(max), '5F4DCC3B5AA765D61D8327DEB882CF99', 2);
_M³;ZØe© ü'¡Çý°æ

相關文章

寫留言

你的電子郵件位址並不會被公開。 必要欄位標記為 *