Is there a way to turn sys.fn_varbintohexstr result back to varbinary?

耗尽温柔 提交于 2019-12-23 12:43:15

问题


Is there a function in SQL Server to do that? to reverse sys.fn_varbintohexstr?


回答1:


select convert(varbinary(max),@hex,2) from foobar




回答2:


You need to use the hexadecimal character string in a dynamic SQL statement, so that it will be parsed as a varbinary. Here's one example of how to do that.

-- Our original and fn_varbintohexstr values:
DECLARE @original varbinary(max) = 0xd0cf11;
DECLARE @sql nvarchar(max) = N'SET @converted = ' + sys.fn_varbintohexstr(@original) + ';';

-- Do the conversion
DECLARE @converted varbinary(max);
EXEC sp_executesql @sql,
   N'@converted varbinary(max) OUTPUT',
   @converted = @converted OUTPUT;

-- Proof it worked
PRINT @original;
PRINT @converted;
PRINT CASE WHEN @original = @converted THEN 'Same' ELSE 'Different' END;

This prints Same.

If you try user1617237's version, you'll see why it is not a correct answer.



来源:https://stackoverflow.com/questions/17818093/is-there-a-way-to-turn-sys-fn-varbintohexstr-result-back-to-varbinary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!