Solved

Convert binary/varbinary(8) to something readable

Posted on 2010-09-03
5
694 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
  • 3
5 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now