Solved

Convert binary/varbinary(8) to something readable

Posted on 2010-09-03
5
698 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
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

832 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