"Converting a varbinary to a varchar " can mean different things.
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
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(varchar(max), @b, 0)
Other style parameters are available with CONVERT for different requirements as noted in other answers.
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
7条答案
按热度按时间zvms9eto1#
The following expression worked for me:
Here are more details on the choice of style (the third parameter).
lb3vh1jj2#
"Converting a
varbinary
to avarchar
" can mean different things.If the varbinary is the binary representation of a string in SQL Server (for example returned by casting to
varbinary
directly or from theDecryptByPassPhrase
orDECOMPRESS
functions) you can justCAST
itThis is the equivalent of using
CONVERT
with a style parameter of0
.Other style parameters are available with
CONVERT
for different requirements as noted in other answers.bbmckpt73#
Actually the best answer is
using "
2
" cuts off the "0x
" at the start of thevarbinary
.vql8enpb4#
Try this
wdebmtf25#
I tried this, it worked for me:
kpbpu0086#
For a
VARBINARY(MAX)
column, I had to useNVARCHAR(MAX)
:Or
quhf5bfb7#
Have a go at the below as I was struggling to
Reference: original post