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', 'P@ssw0rd'), 1);
0x5F4DCC3B5AA765D61D8327DEB882CF99
select convert(varchar(max), HASHBYTES('md5', 'P@ssw0rd'), 2);
5F4DCC3B5AA765D61D8327DEB882CF99
select convert(varbinary(max), '5F4DCC3B5AA765D61D8327DEB882CF99', 2);
_M³;ZØe© ü'¡Çý°æ

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *