在 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
varbinary to string on SQL Server
提问by Bilgin K?l??
How to convert a column value from varbinary(max)
to varchar
in human-readable form?
如何将列值从转换varbinary(max)
到varchar
人类可读的形式?
采纳答案by Martin Smith
"Converting a varbinary
to 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 varbinary
directly or from the DecryptByPassPhrase
or DECOMPRESS
functions) you can just CAST
it
如果 varbinary 是 SQL Server 中字符串的二进制表示形式(例如,通过varbinary
直接强制转换或从DecryptByPassPhrase
或DECOMPRESS
函数返回),您可以只使用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 CONVERT
with a style parameter of 0
.
这等效于使用CONVERT
样式参数0
。
CONVERT(varchar(max), @b, 0)
Other style parameters are available with CONVERT
for different requirements as noted in other answers.
CONVERT
如其他答案中所述,其他样式参数可用于满足不同要求。
回答by Gunjan Juyal
回答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