Solved

Convert binary/varbinary(8) to something readable

Posted on 2010-09-03
5
723 Views
Last Modified: 2012-05-10
I have a table that stores a company id as binary(8) when I try to convert that field into something readable. I seem to get strange results.

I tried the following code:

select company_id,convert(int,company_id) --->if there is a dash "-" in the number it only gives me the number to the right of dash

select company_id,convert(varchar,company_id)  ---> gives me a blank field

Thanks in advance for any help I recieve.
0
Comment
Question by:simpkinst
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33596557
select company_id, convert(varchar(max), company_id)

Can you list some rows for company_id, just to get a feel?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33596607
You can really store anything in binary(8) so unless you know what data went in, it's hard to extract what it should be coming out.


create table #t1b (company_id binary(8));

insert #t1b select 0x12  -- the bytes #18
insert #t1b select 0x121231231231313
insert #t1b select 12345  -- a number
insert #t1b select convert(varbinary(max),'abd-123') -- a string

select company_id,
      datalength(company_id),
      convert(bigint, company_id),
      convert(varchar(max), company_id) from #t1b
0
 

Author Comment

by:simpkinst
ID: 33596742
most of them are stored as just numbers, but a few have numbers dash (- ) numbers and I can't get the information to the left of the dash.
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 33597123
convert(varchar(max), company_id)    should work, as shown below.
Unless your insert/update code is actually incorrect and already losing the data.


create table #t1b (company_id binary(8));

insert #t1b select 0x12  -- the bytes #18
insert #t1b select 0x121231231231313
insert #t1b select 12345  -- a number
insert #t1b select convert(varbinary(max),'456123') -- number stored as a string
insert #t1b select convert(varbinary(max),'456-123') -- number with dash, really a string
insert #t1b select convert(varbinary(max),'abd-123') -- a string

select company_id,
      datalength(company_id),
      convert(bigint, company_id),
      convert(varchar(max), company_id) from #t1b
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33601334
>> I can't get the information to the left of the dash.<<
That would be because you have a CHAR(0) to the left of the dash, otherwise known as a null an not to be confused with the T-SQL NULL.
0

Featured Post

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question