Oracle - 将 Varchar 转换为 Float 并指定精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6101638/
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-18 23:45:17  来源:igfitidea点击:

Oracle - Cast Varchar to Float and specify the precision

oraclecastingoracle10g

提问by contactmatt

I need to cast a varchar to a float. (The varchar is guaranteed to be a number)

我需要将 varchar 转换为浮点数。(varchar 保证是一个数字)

I'm trying to create a materialized view on top of a pre-built table. Because of this, all the data types must match exactly...including the precision and size of the data types. The original column (before the NVL was added) was pulling from a FLOAT data type with a precision of 126. When I try casting a varchar to a float of a precision of 126 it doesn't seem to include the 126 data precision.

我正在尝试在预建表的顶部创建一个物化视图。因此,所有数据类型都必须完全匹配……包括数据类型的精度和大小。原始列(在添加 NVL 之前)从精度为 126 的 FLOAT 数据类型中提取。当我尝试将 varchar 转换为精度为 126 的浮点数时,它似乎不包括 126 数据精度。

(I tested the fact that it wasn't including the 126 data size by creating a standard view with the below query that casts to float(126). Through my Toad IDE I could see that the precision of the "ThisorThat" float column was 38).

(我测试了它不包括 126 数据大小的事实,即通过使用以下转换为 float(126) 的查询创建标准视图。通过我的 Toad IDE,我可以看到“ThisorThat”浮点列的精度是38)。

I have simply updated my materialized view with a NVL statement like so...

我只是用像这样的 NVL 语句更新了我的物化视图......

Select Cast(NVL(thisFloat, thatFloat) as Float(126)) as ThisorThat
....
From tables;

I get the error "Ora-12060: shape of prebuilt table does not match definition query" because the sizes are different than the original table that I am "pre-building" upon. I need to somehow cast the varchar to a float with an explicit size of 126. Any ideas?

我收到错误“Ora-12060:预构建表的形状与定义查询不匹配”,因为大小与我“预构建”的原始表不同。我需要以某种方式将 varchar 转换为显式大小为 126 的浮点数。有什么想法吗?

Version: Oracle 10g

版本:Oracle 10g

Edit:Here's a linkwhich is basically the same thing I'm encountering.

编辑:这是一个链接,它与我遇到的基本相同。

回答by O. Jones

Use

TO_BINARY_FLOAT(mynumberstring) 

or

或者

TO_BINARY_DOUBLE(mynumberstring) 

What you're trying to do actually isn't a cast, it's a conversion.

你真正想做的不是演员表,而是一种转换。