简体   繁体   中英

SQL Server convert varbinary to string

I have a table that has a varbinary field like:

0x83838383838372723 .....

I would like to concatenate this varbinary field to a string, for example:

SELECT CONCAT('Varbinary value is', varbinary_field)
FROM MyTable

I expect the following string:

Varbinary value is 0x83838383838372723

I need to use concat so how to do it?

You might use the built-in function fn_varbintohexstr :

DECLARE @SomeHexString VARBINARY(MAX)=CAST('This is just some text, which should be a HEX-string' AS VARBINARY(MAX));
SELECT @SomeHexString;

SELECT 'This is concatenated: ' + sys.fn_varbintohexstr(@SomeHexString)

This function existed in 2005 already, but was limited in length . Should be fine with your 2008 environment...

Something like this should work. BTW, it seems the varbinary you posted is not really valid.

declare @Bin varbinary(max) = convert(varbinary(max), 'My binary value')

select concat('varbinary value is: ', cast(@Bin as varchar(max)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM