Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite fraction value in sql query column in c#

I'm facing a problem which occurs when trying to get the results of this simple sql query in NET (c#):

select 1/3 as col from dual

OracleDataReader.GetValue(i) throws an exception:

Arithmetic operation resulted in an overflow.
at Oracle.DataAccess.Types.DecimalConv.GetDecimal(IntPtr numCtx)
at Oracle.DataAccess.Client.OracleDataReader.GetDecimal(Int32 i)
at Oracle.DataAccess.Client.OracleDataReader.GetValue(Int32 i)

I've discovered that this error occures when number precision goes over 28 digits, so this leads to error:

select round(1/3,29) as col from dual

but this won't

select round(1/3,28) as col from dual

Trying to treat column value in c# as Double gets error "Invalid cast"

Does anyone know any approach to avoid this situation except of ubiquitous rounding of number columns?

like image 895
Andrey Khataev Avatar asked Nov 04 '22 02:11

Andrey Khataev


1 Answers

Since data type is decimal it must be precise. It's not possible to store 1/3 infinite fraction in a decimal type. In general Conversions from Single or Double to Decimal throw an OverflowException if the result of the conversion is not representable as a Decimal. Thus I need to either read this value as a double or, be sure it's a correct decimal.

like image 62
Petr Abdulin Avatar answered Nov 07 '22 22:11

Petr Abdulin