oracle oracle中将字符串转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/857059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:51:03  来源:igfitidea点击:

Converting a string to an integer in oracle

oracleodp.net

提问by JDunkerley

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

我正在尝试将 Oracle(版本 8i)中的一列字符串解析为整数。

I am accessing the results through Oracle.DataAccess library

我正在通过 Oracle.DataAccess 库访问结果

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

我已经在使用TO_NUMBER掩码将字符串转换为没有小数位的数字。问题是客户端代码中的值被检索为十进制而不是整数。

采纳答案by Aaron Daniels

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.

在 ODP.NET 中,NUMBER 列总是以小数形式返回。要解决这个问题,请将其作为 OracleDecimal 拉回来,它有几个“Toxxxx”方法可以将值转换为您需要的本机 .NET 类型。

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.)

(如果上面的代码不是 100% 正确,请原谅我,因为我家里没有安装 ODP.NET。)

回答by Patrick McDonald

CAST(field AS integer)

回答by Hank Gay

TO_NUMBERis what you want, specifically the TO_NUMBER('42', '99')version.

TO_NUMBER是您想要的,特别是TO_NUMBER('42', '99')版本。

回答by Jordi Cabot

You can always turn the decimal into an integer using the round function (or the trunc or floor functions)

您始终可以使用 round 函数(或 trunc 或 floor 函数)将小数转换为整数