Link to home
Start Free TrialLog in
Avatar of marshmeli
marshmeli

asked on

Convert a VarBinary to a VarChar

Hey,

According to the MSDN [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_2f3o.asp] the conversion from VarBinary to VarChar can be done but, for me, it returns non-ascii chars...

I think the question has been asked her before but I cannot find the solutions.  Any help will be great, and as you can tell from the Topic, this is for MS SQL Server.
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

>>the conversion from VarBinary to VarChar can be done but, for me, it returns non-ascii chars...<<
You can use CAST or CONVERT.  varchar only supports ASCII characters.  Please define what you mean by "non-ascii chars".  
DECLARE @X varbinary(20)
Set @X = CAST('Experts Exchange' As varbinary(20))
Select @X As [varbinary], CAST(@X as varchar(20)) As [varchar]

This gives me the following result:
varbinary                                  varchar              
------------------------------------------ --------------------
0x457870657274732045786368616E6765         Experts Exchange
ASKER CERTIFIED SOLUTION
Avatar of puranik_p
puranik_p
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marshmeli
marshmeli

ASKER

I will try those solutions out today or tomorrow and get back to you...

Acperkins, this is what i mean by non-ascii:

this is my SQL statement I use:
select cast(duration as varbinary), duration from asdb
where substring(identifier, 1, 7) = 'TMV199A'
order by identifier

a result i get it:
=================
0x00152920  |  1386784

which is correct 0x00152920 is the rep. of the hex number 1386784, which was stored as an integer...

now i need to get that vabinary result (0x00152920) to be returned as a varchar string "0x00152920" so that I can manipulate as I need to....

Oh I forgot to add

if I tried to convert or cast the varbinary result to a varchar I get
=======================
)         |          1386784
Also, found out that you can  take a substring of a varbinary...

so i was able to do something like this:
(substring(cast(duration as varbinary), 1, 1)

that way i do not have to use the undocumented function...