Solved

Converting a Binary to a Varchar

Posted on 2002-04-14
14
2,184 Views
Last Modified: 2012-08-13
I am trying to convert a binary number to a varchar so I can include it into a string.  If do a straight Convert then I get blank, if I convert to an integer I get a different number.  Help tells me that converting from a binary to an integer and then converting back to a binary will not result in the same binary number that you started out with.

I need to know if anyone has been able to successfully convert a binary to a varchar and back again, and how they did it.....
0
Comment
Question by:Garfy
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 4

Expert Comment

by:urim
ID: 6941277
Why not use cast (@a as int)

as in the next example:

declare @a binary(16), @b bigint

select @a = 2147483647
select @b = cast (@a as int)
select @a, @b

Regards,
Uri
0
 
LVL 1

Author Comment

by:Garfy
ID: 6943433
Read the question again, I have already tried that, and Microsoft themselves do not recommend this course of action, but they do not give a solution......
0
 
LVL 1

Author Comment

by:Garfy
ID: 6943434
Read the question again, I have already tried that, and Microsoft themselves do not recommend this course of action, but they do not give a solution......
0
 
LVL 1

Author Comment

by:Garfy
ID: 6943488
print @CompanyID returns 0x0000000000002E66


set @Message = 'Company No: ' + Convert(varchar, Cast (@CompanyID as bigint))
Print @Message

returns

Company No: 11878
0
 
LVL 3

Expert Comment

by:bobj_kirk
ID: 6943955
Garfy,

Well, I am not sure of your purpose, but it is an interesting challenge, so I wrote some code for you. Here it is:

declare @CompanyID binary(8)
declare @Byte int
declare @Digit int
declare @String varchar(30)
declare @I int

select @CompanyID = 0x0000000000002E66

select @I=1
select @String = 'Value is 0x'
while @I < 9
  begin
  select @Byte = substring(@CompanyID, @I, 1)
  select @Digit = floor(@Byte / 16)
  select @String = @String + substring('0123456789ABCDEF', @Digit + 1, 1)
  select @Digit = @Byte % 16
  select @String = @String + substring('0123456789ABCDEF', @Digit + 1, 1)
  select @I = @I + 1
  end

select @string as 'Converted String'

This code returns the following:
Converted String              
------------------------------
Value is 0x0000000000002E66

(1 row(s) affected)

BTW, the value 0x0000000000002E66 is a hexadecimal number equal to 11878 decimal as your query shows. You can confirm it by using windows calculator in scientific mode to convert the decimal number to hex.

Hope this helps.

-Bob

0
 
LVL 1

Author Comment

by:Garfy
ID: 6944012
Sadly, by using substring, and I was still not able to add the binary number to another string.......  it keeps saying that you cannot add a string and binary.....    I am using SQL 2000.  Microsoft appear to be aware of a problem with binaries and strings but they have not posted a resolution.  I am hoping someone here has one...  

All I want to do is run these lines

********************************************************
/*set @Message = ('Update ' + @Tname + ' set ' + @Fname + ' = ' + Convert(varchar,@CompanyID) + ' where ' + @Fname + ' = ' + CONVERT(varchar, @ContactID))

exec (@Message)
*********************************************************

CompanyID and ContactID are binary numbers.......
0
 
LVL 4

Expert Comment

by:urim
ID: 6944036
Garfy,

If this what you looking for:

declare @a binary(16)
select @a = 0x0000000000002E66
select @a, '<' + dbo.fn_sqlvarbasetostr(@a) + '>'

output:
0x0000000000002E660000000000000000     <0x0000000000002e660000000000000000>

Uri
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 4

Expert Comment

by:urim
ID: 6944045
or even better use dbo.fn_varbintohexstr(@a)
0
 
LVL 1

Author Comment

by:Garfy
ID: 6944081
I got so excited....  but then I got this error message

Server: Msg 208, Level 16, State 1, Procedure rc_spCompIDsameContIDnotOrphans, Line 143
Invalid object name 'dbo.fn_varbintohexstr'.
0
 
LVL 3

Expert Comment

by:bobj_kirk
ID: 6944173
Garfy,

I am only using SQL 7 and I don't know why you are having so much trouble with SQL 2000. Anyway, here's a hack which works on SQL 7 and doesn't use substring on binary data:

declare @CompanyID binary(8)
declare @Binary binary(4)
declare @Integer int
declare @Decimal decimal(20)
declare @Digit int
declare @String varchar(30)
declare @I int

select @CompanyID = 0x0000000000002E66
select @Binary = @CompanyID
select @Integer = @Binary
select @Decimal = convert(decimal, @Integer) * power(convert(decimal,2), 32)
select @Integer = @CompanyID
select @Decimal = @Decimal + convert(decimal, @Integer)
if @Integer < 0 select @Decimal = @Decimal + power(convert(decimal, 2), 32)

select @I=1
select @String = ''
while @I < 17
 begin
 select @Digit = @Decimal - floor(@Decimal / 16) * 16
 select @String = substring('0123456789ABCDEF', @Digit + 1, 1) + @String
 select @Decimal = floor(@Decimal / 16)
 select @I = @I + 1
 end
Select @String = 'Value is 0x' + @String

select @string as 'Converted String'

Converted String              
------------------------------
Value is 0x0000000000002E66

(1 row(s) affected)


It only works with data up to 8 bytes (2 integers) long, but it could be extended by adding more interim binary variables. Not elegant code, but may help.

-Bob
0
 
LVL 4

Accepted Solution

by:
urim earned 200 total points
ID: 6944179
write master.dbo.fn_varbintohexstr
0
 
LVL 1

Author Comment

by:Garfy
ID: 6946439
thank you thank you thank you.  You are a marvel.  Is there a function that does the reverse??????
0
 
LVL 1

Author Comment

by:Garfy
ID: 6946441
thank you thank you thank you.  You are a marvel.  Is there a function that does the reverse??????
0
 
LVL 6

Expert Comment

by:JohnDesautels
ID: 13872773
I hunted for a way to concatenate a binary field with text.

Master.dbo.fn_varbintohexstr worked for me!
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculated columns 13 61
SQL Error in WHERE Clause 5 39
Get Duration of last Status Update 4 29
Sql Count with Select Distinct 4 26
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

16 Experts available now in Live!

Get 1:1 Help Now