patternMinor
Converting VARBINARY HEX to VARCHAR
Viewed 0 times
varbinaryconvertinghexvarchar
Problem
I have a fun problem,
I have data stored in SQL Server 2008
I need to see what this translated to string is.
I have tried
If I put the value that is in my
Any help would be appreciated.
Details.
Example of the Hex is
It should work out to.
EDIT: some additional information. This data starts out in ORACLE it is in a
We bring the data to SQL Server 2008 via Linked Servers using the below open query.
When I use the suggested function on this SQL Server 2008 table below.
I get these symbols:
If I do the conversion of HEX in Oracle first and then try and bring it over I get an empty column.
Lastly, in the first instance with no conversion in the oracle table the data type for the column
When I try the conversion in Oracle first is
I have data stored in SQL Server 2008
VARDATA column as Hex.I need to see what this translated to string is.
I have tried
CONVERT(VarChar(MAX), VARDATA, 2) and it returns a Varchar value of the hex exactly the same as it is in the VARDATA.If I put the value that is in my
VARDATA into an online converter I get the exact value I expect back.Any help would be appreciated.
Details.
Example of the Hex is
0x003200300031003400300037003200340030003000300035003600380038003300300039It should work out to.
201407240005688309EDIT: some additional information. This data starts out in ORACLE it is in a
RAW type column.We bring the data to SQL Server 2008 via Linked Servers using the below open query.
Select TABNAME, VARKEY, DATALN, VARDATA
from sapsr3.KAPOLWhen I use the suggested function on this SQL Server 2008 table below.
Select CONVERT(NVARCHAR(MAX),VARDATA)
from KAPOLI get these symbols:
㈀ 㜀㜀㠀㠀If I do the conversion of HEX in Oracle first and then try and bring it over I get an empty column.
Select TABNAME, VARKEY, DATALN, utl_raw.cast_to_nvarchar2(hextoraw(VARDATA)) VARDATA
from sapsr3.KAPOLLastly, in the first instance with no conversion in the oracle table the data type for the column
VARDATA is VARBINARYWhen I try the conversion in Oracle first is
NVARCHARSolution
You need to have a distinction between
I get this value, which looks kind of like the value you're converting, but not quite:
So now if I change the input to be
Now I do get the value you wanted:
So if I take that value and try to convert it to
Result:
The online converter you're using must be using Unicode behind the scenes. You need to do that, too.
varchar and nvarchar. There is a big difference. If I try to create varbinary from your "should work out to" value:SELECT CONVERT(VARBINARY(64), '201407240005688309')I get this value, which looks kind of like the value you're converting, but not quite:
0x323031343037323430303035363838333039So now if I change the input to be
nvarchar:SELECT CONVERT(VARBINARY(64), N'201407240005688309')Now I do get the value you wanted:
0x3200300031003400300037003200340030003000300035003600380038003300So if I take that value and try to convert it to
nvarchar instead of varchar, we're back where we started:SELECT CONVERT(NVARCHAR(MAX),
0x3200300031003400300037003200340030003000300035003600380038003300300039)Result:
201407240005688309The online converter you're using must be using Unicode behind the scenes. You need to do that, too.
Code Snippets
SELECT CONVERT(VARBINARY(64), '201407240005688309')0x323031343037323430303035363838333039SELECT CONVERT(VARBINARY(64), N'201407240005688309')0x3200300031003400300037003200340030003000300035003600380038003300SELECT CONVERT(NVARCHAR(MAX),
0x3200300031003400300037003200340030003000300035003600380038003300300039)Context
StackExchange Database Administrators Q#97445, answer score: 4
Revisions (0)
No revisions yet.