?
Solved

Convert binary/varbinary(8) to something readable

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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.
Suggested Courses

764 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