Author 
Topic 

ishpal
Starting Member
Canada
6 Posts 
Posted  06/27/2013 : 10:44:07

We are currently dealing with a legacy application, where the decimal numbers from c# are stored in the database (sqlserver) as varbinary types. This (I think) was done to keep formatting with the number.
The problem now is that we can not search/index on the number in the database. It has to be restored to a c# decimal in the application and then only does this make sense.
How can I convert the varbinary to a decimal/numeric type in the sqlserver?
I don't mind creating a new column/table to store the numeric value and formatting information derived from the varbinary.
I know in c# you can create a decimal number by giving it an array of ints.
Here is the description of how c# interprets and converts an int array to decimal type. http://msdn.microsoft.com/enus/library/aa326746 v=vs.71 .aspx
The binary representation of a Decimal number consists of a 1bit sign, a 96bit integer number, and a scaling factor used to divide the integer number and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28. bits is a fourelement long array of 32bit signed integers. bits [0], bits 1, and bits [2] contain the low, middle, and high 32 bits of the 96bit integer number. bits [3] contains the scale factor and sign, and consists of following parts: Bits 0 to 15, the lower word, are unused and must be zero. Bits 16 to 23 must contain an exponent between 0 and 28, that indicates the power of 10 to divide the integer number. Bits 24 to 30 are unused and must be zero. Bit 31 contains the sign; 0 meaning positive, and 1 meaning negative. A numeric value might have several possible binary representations; all are equally valid and numerically equivalent. Note that the bit representation differentiates between negative and positive zero. These values are treated as being equal in all operations.
Help is highly appreciated. 

James K
Flowing Fount of Yak Knowledge
3744 Posts 
Posted  06/27/2013 : 11:03:12

You can try casting the varbinary to varchar. For example:SELECT CAST(YourVarBinaryCol AS VARCHAR(MAX)) 


Lamprey
Flowing Fount of Yak Knowledge
4614 Posts 
Posted  06/27/2013 : 11:16:45

If the data is stored as a string/varchar, what is the format? You have mentioned Decimal and Binary so I'm not sure how it is actually stored. If it is just a decimal number (123456.789 for example) then a straight CAST or CONVERT will work if specify the appropriate size: SELECT CAST(YourDecimalCol AS DECIMAL(19,2))
If it is stored as binary or hex or something, then that is a slightly different beast.
If it in not stored as a DECIMAL, then please post sample data and expected output. There ar esome links that can help you provide that: http://www.sqlservercentral.com/articles/Best+Practices/61537/ http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx



ishpal
Starting Member
Canada
6 Posts 
Posted  06/27/2013 : 11:33:49

Here is some sample data. Name DecimalValue NumericValue  Number2_4 0x14E20100000000000000000000000200 1234.12 Number1 0xCF040000000000000000000000000100 123.1 Number5 0xD9299549000000000000000000000500 12345.12345 Number4 0xF24FBC00000000000000000000000400 1234.1234
SQL test harness ================= ;With MySampleData([Name],[DecimalValue],[NumericValue]) AS ( SELECT 'Number2_4', 0x14E20100000000000000000000000200, 1234.12 UNION ALL SELECT 'Number1', 0xCF040000000000000000000000000100, 123.1 UNION ALL SELECT 'Number5', 0xD9299549000000000000000000000500, 12345.12345 UNION ALL SELECT 'Number4', 0xF24FBC00000000000000000000000400, 1234.1234 ) SELECT * FROM MySampleData
C# code for convertion ======================= byte[] GetBytes ( decimal? value ) { if ( value == null ) { return null; } byte[] bytes = new byte[16]; int[] bits = Decimal.GetBits ( (decimal) value ); Array.Copy ( BitConverter.GetBytes ( bits[0] ), 0, bytes, 0, 4 ); Array.Copy ( BitConverter.GetBytes ( bits[1] ), 0, bytes, 4, 4 ); Array.Copy ( BitConverter.GetBytes ( bits[2] ), 0, bytes, 8, 4 ); Array.Copy ( BitConverter.GetBytes ( bits[3] ), 0, bytes, 12, 4 ); return bytes; }
decimal? ToDecimal ( byte[] value ) { if ( value == null ) { return null; } int[] bits = { BitConverter.ToInt32 ( value, 0 ), BitConverter.ToInt32 ( value, 4 ), BitConverter.ToInt32 ( value, 8 ), BitConverter.ToInt32 ( value, 12 ) }; return new decimal ( bits ); } 
Edited by  ishpal on 06/27/2013 11:57:52 


Lamprey
Flowing Fount of Yak Knowledge
4614 Posts 
Posted  06/27/2013 : 13:22:33

This can be simplified, but shows how it can be done:SELECT
(Number / (POWER(10, Exponent) * 1.0)) * CASE WHEN SignIndicator = 0 THEN 1 ELSE  1 END
FROM
(
SELECT
CAST(CONVERT(VARBINARY(MAX), '0x' + High + Mid + Low, 1) AS INT) AS Number
,CAST(CONVERT(VARBINARY(MAX), '0x' + Exponent, 1) AS INT) Exponent
,CAST(CONVERT(VARBINARY(MAX), '0x0' + SignIndicator, 1) AS INT) AS SignIndicator
FROM
(
SELECT
LEFT(RIGHT('0x14E20100000000000000000000000200', 32), 2) AS Low
,LEFT(RIGHT('0x14E20100000000000000000000000200', 30), 2) AS Mid
,LEFT(RIGHT('0x14E20100000000000000000000000200', 28), 2) AS High
,SUBSTRING('0x14E20100000000000000000000000200',31, 2) AS Exponent
,SUBSTRING('0x14E20100000000000000000000000200',33, 1) AS SignIndicator
) AS A
) AS B



James K
Flowing Fount of Yak Knowledge
3744 Posts 
Posted  06/27/2013 : 13:26:15

Very nice indeed, Lamprey! I looked at it for a few minutes, shook my head in frustration, and walked away from it :) 


Lamprey
Flowing Fount of Yak Knowledge
4614 Posts 
Posted  06/27/2013 : 13:33:38

The the whole bit / byte thing really makes in confusing. :) 


ishpal
Starting Member
Canada
6 Posts 
Posted  06/27/2013 : 14:34:37

Thanks Lamprey! Very impressive. 


ishpal
Starting Member
Canada
6 Posts 
Posted  06/27/2013 : 15:25:15

Hi Lamprey,
There seems to be a problem, 0xD9299549000000000000000000000500 should get 12345.12345, but instead gets 97.75577000000000.
SELECT (Number / (POWER(10, Exponent) * 1.0)) * CASE WHEN SignIndicator = 0 THEN 1 ELSE  1 END FROM ( SELECT CAST(CONVERT(VARBINARY(MAX), '0x' + High + Mid + Low, 1) AS INT) AS Number ,CAST(CONVERT(VARBINARY(MAX), '0x' + Exponent, 1) AS INT) Exponent ,CAST(CONVERT(VARBINARY(MAX), '0x0' + SignIndicator, 1) AS INT) AS SignIndicator FROM ( SELECT LEFT(RIGHT('0xD9299549000000000000000000000500', 32), 2) AS Low ,LEFT(RIGHT('0xD9299549000000000000000000000500', 30), 2) AS Mid ,LEFT(RIGHT('0xD9299549000000000000000000000500', 28), 2) AS High ,SUBSTRING('0xD9299549000000000000000000000500',31, 2) AS Exponent ,SUBSTRING('0xD9299549000000000000000000000500',33, 1) AS SignIndicator ) AS A ) AS B



Lamprey
Flowing Fount of Yak Knowledge
4614 Posts 
Posted  06/27/2013 : 18:08:06

Indeed it does. Can you describe how to convert the number? bascially you told me the first 3 "bits" comprise the number. But, that number requires 4 "bits." You basically need to reverse the order the 2"bit" combinations, so you can keep adding to what I did previosuly:SELECT
(Number / (POWER(10, Exponent) * 1.0)) * CASE WHEN SignIndicator = 0 THEN 1 ELSE  1 END
FROM
(
SELECT
CAST(CONVERT(VARBINARY(MAX), '0x' + Higher + High + Mid + Low, 1) AS INT) AS Number
,CAST(CONVERT(VARBINARY(MAX), '0x' + Exponent, 1) AS INT) Exponent
,CAST(CONVERT(VARBINARY(MAX), '0x0' + SignIndicator, 1) AS INT) AS SignIndicator
FROM
(
SELECT
LEFT(RIGHT('0xD9299549000000000000000000000500', 32), 2) AS Low
,LEFT(RIGHT('0xD9299549000000000000000000000500', 30), 2) AS Mid
,LEFT(RIGHT('0xD9299549000000000000000000000500', 28), 2) AS High
,LEFT(RIGHT('0xD9299549000000000000000000000500', 26), 2) AS Higher
,SUBSTRING('0xD9299549000000000000000000000500',31, 2) AS Exponent
,SUBSTRING('0xD9299549000000000000000000000500',33, 1) AS SignIndicator
) AS A
) AS B



ishpal
Starting Member
Canada
6 Posts 
Posted  06/28/2013 : 10:32:22

Thanks Lamprey! This works fine. I was trying numbers bigger than int and then bigger than bigint :)
Also, the documentation was copied from MSDN decimal and it is not easily understood.
Regards, 



Topic 


