Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a string to an integer in oracle

Tags:

oracle

odp.net

I am trying to parse a column of strings in Oracle (version 8i) to an integer.

I am accessing the results through Oracle.DataAccess library

I'm already using TO_NUMBER with a mask to convert the string into a number with no decimal places. The problem is that the value in the client code is being retrieved as a decimal rather than an int.

like image 692
JDunkerley Avatar asked Dec 22 '22 11:12

JDunkerley


2 Answers

CAST(field AS integer)
like image 74
Patrick McDonald Avatar answered Jan 30 '23 23:01

Patrick McDonald


NUMBER columns always come back as decimals in ODP.NET. To get around this, pull it back as an OracleDecimal, which has several "Toxxxx" methods to cast the value into the native .NET type you need.

while (myOracleDataReader.Read())
{
    int x = myOracleDataReader.GetOracleDecimal(0).ToInt32();
}

(Forgive me if the code above isn't 100% correct, as I don't have ODP.NET installed at home.)

like image 27
Aaron Daniels Avatar answered Jan 30 '23 22:01

Aaron Daniels