Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Converting varbinary to numeric type in tsql -- decimal in c#


Converting varbinary to numeric type in tsql -- decimal in c#

Author
Message
isingh
isingh
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 59
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/en-us/library/aa326746(v=vs.71).aspx

The binary representation of a Decimal number consists of a 1-bit sign, a 96-bit 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 four-element long array of 32-bit signed integers. bits [0], bits 1, and bits [2] contain the low, middle, and high 32 bits of the 96-bit 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.
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14971 Visits: 38985
i'm not sure if this will help, I'd really like to see a handful of sample data rows, and their expected values to really be sure.
but can you convert them directly to a decimal and back? or is there more to it than that?


select convert(varbinary,8789961.4513)
select convert(decimal(19,4),0x0B04000131AD397714000000)



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

isingh
isingh
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 59
If it was simple convertion/cast it would not be a problem.

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
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14971 Visits: 38985
yeah i see that now;
a simple test harness for others to use:

;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



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16626 Visits: 17024
Can you post the c# code that does the conversion?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
isingh
isingh
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 59
Here is the c# code for converting to and from the bytes


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 );
}
isingh
isingh
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 59
This issue is fully resolved by Lamprey! on sqlteam forum. See answer by visiting

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=186457
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search