在 SQL Server 上将 varbinary 转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3289988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:53:12  来源:igfitidea点击:

varbinary to string on SQL Server

sqlsql-serversql-server-2005tsql

提问by Bilgin K?l??

How to convert a column value from varbinary(max)to varcharin human-readable form?

如何将列值从转换varbinary(max)varchar人类可读的形式?

采纳答案by Martin Smith

"Converting a varbinaryto a varchar" can mean different things.

“将 a 转换varbinary为 a varchar”可能意味着不同的事情。

If the varbinary is the binary representation of a string in SQL Server (for example returned by casting to varbinarydirectly or from the DecryptByPassPhraseor DECOMPRESSfunctions) you can just CASTit

如果 varbinary 是 SQL Server 中字符串的二进制表示形式(例如,通过varbinary直接强制转换或从DecryptByPassPhraseDECOMPRESS函数返回),您可以只使用CAST

declare @b varbinary(max)
set @b = 0x5468697320697320612074657374

select cast(@b as varchar(max)) /*Returns "This is a test"*/

This is the equivalent of using CONVERTwith a style parameter of 0.

这等效于使用CONVERT样式参数0

CONVERT(varchar(max), @b, 0)

Other style parameters are available with CONVERTfor different requirements as noted in other answers.

CONVERT如其他答案中所述,其他样式参数可用于满足不同要求。

回答by Gunjan Juyal

The following expression worked for me:

以下表达式对我有用:

SELECT CONVERT(VARCHAR(1000), varbinary_value, 2);

Hereare more details on the choice of style (the third parameter).

这里有更多关于风格选择的细节(第三个参数)。

回答by Lara Mayugba

Actually the best answer is

其实最好的答案是

SELECT CONVERT(VARCHAR(1000), varbinary_value, 1);

using "2" cuts off the "0x" at the start of the varbinary.

使用“ 2”切断.0x开头的“ ” varbinary

回答by dmajkic

Try this

尝试这个

SELECT CONVERT(varchar(5000), yourvarbincolumn, 0)

回答by kristoffer_o

For a VARBINARY(MAX)column, I had to use NVARCHAR(MAX):

对于一VARBINARY(MAX)列,我必须使用NVARCHAR(MAX)

cast(Content as nvarchar(max))

Or

或者

CONVERT(NVARCHAR(MAX), Content, 0)
VARCHAR(MAX) didn't show the entire value

回答by Bala

I tried this, it worked for me:

我试过这个,它对我有用:

declare @b2 VARBINARY(MAX) 
set @b2 = 0x54006800690073002000690073002000610020007400650073007400
SELECT CONVERT(nVARCHAR(1000), @b2, 0);

回答by ribbit

Have a go at the below as I was struggling too original post [Here][1]

试试下面的内容,因为我太原始的帖子[这里][1]

bcp "SELECT CAST(BINARYCOL AS VARCHAR(MAX)) FROM OLTP_TABLE WHERE ID=123123 AND COMPANYID=123" 
queryout "C:\Users\USER\Documents\ps_scripts\res.txt" -c -S myserver.db.com  -U admin -P password


  [1]: https://stackoverflow.com/questions/60525910/powershell-truncating-sql-query-output?noredirect=1#comment107077512_60525910