Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
How do you convert a binary data type colume value to a numeric value?

For example:
Convert 000101010101 (binary) to 341 (decimal) in Sql Server 2005
Posted
Updated 28-Jun-11 8:48am
v2
Comments
Sergey Alexandrovich Kryukov 28-Jun-11 12:19pm    
There is not non-binary values. What's 000101010101? A string "000101010101"?
--SA

According to the chart almost halfway down on this page[^], there is an implicit conversion between binary and almost all numeric types (except real and float).
 
Share this answer
 
If your binary value is in string (as SA asked), below code will help you:
int i = Convert.ToInt32("000101010101", 2); here 2 (second parameter) denotes the base of the first parameter (base for binary would be 2). For further help you can read the MSDN documentation for this method.

For Sql Server have a look at this link. This UDF works well when supplied digits only (in the input string) except the case when you want hexadecimal conversion, so take care of that in your calling code.

Hope that helps!
 
Share this answer
 
v2
Comments
RDBurmon 30-Jun-11 4:23am    
Thanks But i need it in sql server 2005
RakeshMeena 30-Jun-11 7:16am    
Answer updated!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900