[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Convert binary/varbinary(8) to something readable

Posted on 2010-09-03
5
Medium Priority
?
745 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

650 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