Converting Binary To Decimal
Hello: I have to convert from Binary column data to Decimal before I display the report. I tried format in SSRs and did not worked. Thanks for your help
July 19th, 2011 3:50pm

Hello, You need to exactly do opposite of the below code:- " If by decimal you just mean a normal decimal number, as opposed to a decimal type, and it is an integer, then to convert to binary is extremely easy. All you have to do is repeatedly divide by 2 until the answer is 0 and look at the remainders. e.g. to convert 50 in decimal to binary Divide by 2 Answer = 25, Remainder = 0, so least significant digit = 0 Divide answer by 2 Answer = 12, Remainder = 1 , so second digit = 1 divide answer by 2 Answer = 6, Remainder = 0, so third digit = 0 divide answer by 2 Answer = 3, Remainder = 0, so fourth digit = 0 divide answer by 2 Answer = 1, Remainder = 1, so fifth digit = 1 divide answer by 2 Answer = 0, Remainder = 1, so sixth digit = 1 So, reading the remainders from bottom to top, 50 in binary is 110010 That should be very easy to code using the Mod operator and Int function. " Please do let us know your feedback. Thanks KumarKG, MCTS
Free Windows Admin Tool Kit Click here and download it now
July 19th, 2011 3:54pm

Hello Again, Why don't you try doing conversation at t-sql level, something like this:- DECLARE @myval decimal (4, 2), @myvalue1 varbinary(8) SET @myval = 91.09 --get varbinary value SELECT @myvalue1 = CAST(@myval AS varbinary(8)) SELECT @myvalue1 --get decimal value SELECT CONVERT(decimal(4,2), @myval) SELECT CONVERT(decimal(4,2), @myvalue1) Thanks KumarKG, MCTS
July 19th, 2011 3:56pm

Hi Kumar: I tried like you said But I am getting error. Price column is in DB is binary(50). This is in SQL server 2000. select Convert(decimal(4,2), price ) from TableName. I am getting the following error "Arithmetic overflow error converting varbinary to data type numeric."
Free Windows Admin Tool Kit Click here and download it now
July 19th, 2011 4:28pm

Hi SACSSP, The Decimal function syntax is: “Decimal[ (p[,s] ) ]”. The “p” indicates the maximum total number of decimal digits that can be stored, both to the left and to the right of the decimal point. The “s” indicates the maximum number of decimal digits that can be stored to the right of the decimal point. For more information about decimal and numeric (Transact-SQL), please refer to the article below: http://msdn.microsoft.com/en-us/library/ms187746.aspx Generally, the error message occurs when we are trying to pass a numeric value that is too large. For example, trying to pass 12345.67 into a Decimal(4,2) would generate the error. Based on your environment, you can try to use the expression below: select Convert(decimal(18,2), price ) from TableName. If you have any more questions about T-SQL, I recommend you post a question in T-SQL forum. It is appropriate and more experts will assist you. http://social.technet.microsoft.com/Forums/en-us/transactsql/threads Thanks, Bin Long Forum Support Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com
July 20th, 2011 6:05am

Hi Bin: I tried this select Convert(decimal(18,2), price ) from TableName but still getting the same error. I posted the same question in TSQL forums. Thanks for your help.
Free Windows Admin Tool Kit Click here and download it now
July 20th, 2011 5:41pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics