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.
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.
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
-------------------------- ---------- ------ --------------------
0x457870657274732045786368 616E6765 Experts Exchange
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
--------------------------
0x457870657274732045786368
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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....
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....
ASKER
Oh I forgot to add
if I tried to convert or cast the varbinary result to a varchar I get
=======================
) | 1386784
if I tried to convert or cast the varbinary result to a varchar I get
=======================
) | 1386784
ASKER
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...
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...
You can use CAST or CONVERT. varchar only supports ASCII characters. Please define what you mean by "non-ascii chars".