SQL Server将varbinary转换为字符串

时间:2021-05-04 23:43:23

I have a table that has a varbinary field like:

我有一个表有一个varbinary字段,如:

0x83838383838372723 .....

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

我想将这个varbinary字段连接到一个字符串,例如:

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?

我需要使用concat所以如何做到这一点?

2 个解决方案

#1


1  

You might use the built-in function fn_varbintohexstr:

您可以使用内置函数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...

该功能已于2005年存在,但篇幅有限。 2008年的环境应该没问题......

#2


-1  

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

这样的事情应该有效。顺便说一下,你发布的varbinary似乎并不真正有效。

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

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

#1


1  

You might use the built-in function fn_varbintohexstr:

您可以使用内置函数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...

该功能已于2005年存在,但篇幅有限。 2008年的环境应该没问题......

#2


-1  

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

这样的事情应该有效。顺便说一下,你发布的varbinary似乎并不真正有效。

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

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