March 14, 2011 at 12:30 pm
Hello, i am trying to convert a binary value to a varchar and I am getting incorrect results. Here is what I am doing:
select distinct
CONVERT(VARCHAR(100), h.HRCHY_ID, 1) AS fConvert,
CAST(h.HRCHY_ID AS VARCHAR(100)) AS fCast,
h.HRCHY_DESC
from
ORG_CHN_HRCHY h JOIN csaRoleLocationAttributes r on
where
h.ACTV = 1
And R.Retailer_Id = 7
and r.role_id = 'FnF-POWER'
and CONVERT(VARCHAR(100), h.HRCHY_ID, 1) = r.LocationHierarchyID
Notice that the CAST function seems to be incorrect. Any ideas on how to correct this? I need to be able to use CAST in order to have common code across RDBMS platforms. Thank you.
David
Best Regards,
~David
March 14, 2011 at 1:13 pm
I have been using master.sys.fn_varbintohexsubstring() to convert varbinary to a hexadecimal string value.
Some people get bent out of shape because it is supposedly "undocumented" but it works fine and I use it in several scripts without issue.
if, on the other hand, the (var)binary data actually contains valid ascii characters you can just use CAST.
The probability of survival is inversely proportional to the angle of arrival.
March 14, 2011 at 1:27 pm
Okay, my initial query was incorrect. Here is the actual query and the incorrect CAST results:
select distinct
CONVERT(VARCHAR(100), h.HRCHY_ID, 1) fConvert,
CAST(h.HRCHY_ID AS VARCHAR(100)) fCast
from
ORG_CHN_HRCHY h,
csaRoleLocationAttributes r
where
h.ACTV = 1
and R.Retailer_Id = 7
and r.role_id = 'FnF-POWER'
and CONVERT(VARCHAR(100), h.HRCHY_ID, 1) = r.LocationHierarchyID
Results:
fConvert fCast
0xFD951AA3A39B9D4EA41646E6DB06C255ý•££›N¤FæÛÂU
The fCast column should be the same as the fConvert. I was hoping to use CAST as it is available in both SQL server and Oracle.
Best Regards,
~David
March 14, 2011 at 2:18 pm
i believe binary must be converted to nvarchar, right? so the conversion to varchar is failing?
select distinct
CONVERT(NVARCHAR(100), h.HRCHY_ID, 1) fConvert,
CAST(h.HRCHY_ID AS NVARCHAR(100)) fCast
from
ORG_CHN_HRCHY h,
csaRoleLocationAttributes r
where
h.ACTV = 1
and R.Retailer_Id = 7
and r.role_id = 'FnF-POWER'
and CONVERT(NVARCHAR(100), h.HRCHY_ID, 1) = r.LocationHierarchyID
Lowell
March 14, 2011 at 2:18 pm
perhaps the string is in UNICODE (or some other encoding?) If so you would need to say:
CAST(h.HRCHY_ID AS NVARCHAR(100)) fCast
The probability of survival is inversely proportional to the angle of arrival.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply