问题
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