tags:

views:

253

answers:

1

In SQL, the following works cast([comlum] as Decimal(18,4))

How can I do the same conversion (from the binary data) in C#?


the data type of varbinary in SQL C# loading to byte[] so, I want to byte[] convert to decimal or double?

Sql query: select cast([column_A] as decimal(18,4), [column_A] from table

result: 191128.0000      0x1204000180D1EB71

I hope to get value as 191128.0000 in C#


Because, I use entity framework
so, have this question

+3  A: 

Well, what does the binary array contain? Is it an existing decimal represented as a byte[]? Unlike things like float / double, there is no common/shared understanding of decimal as byte[].

But if it is a .NET decimal you could use the overload of new decimal(int[]) - but you'll need to pack the bytes into ints first; which means you need to know the endianness...

Or is the array actually a double? In which case, BitConverter should help.


Having seen the update to the question, I suspect that you are going to have to cast this data (in TSQL) before it comes out of SQL Server, or dig out the implementation-specific encoding used to pack the data in SQL Server (which would be brittle, and IMO probably shouldn't leave the server). There is a SqlDecimal type, but this also doesn't accept a byte[].

Marc Gravell