Created
April 16, 2014 20:43
-
-
Save bitwalker/10931039 to your computer and use it in GitHub Desktop.
Convert a hex-encoded string to varbinary - MSSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare @hexstring varchar(max) | |
declare @bin varbinary(max) | |
set @hexstring = '0x10.....' | |
set @bin = ( | |
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) | |
) | |
select @bin | |
update db.dbo.table | |
set column = @bin | |
where id = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks a lot, built-in function of mssql has input length limit, such as convert and fn_cdc_hexstrtobin