2017-06-02 21 views
1

我正在寻找一种方法来查找Oracle数据库表格中FLOAT特定列中的值有8位以上有效数字的所有行。查找其中FLOAT数字有8位以上有效数字的所有行使用SQL

我在想,可能有一些聪明的数学方法来识别行,但我不能想到任何。

下面的数字应该被接受:

  • 123456789.123
  • 123456789.01
  • 0.123456789

但并不是以下

  • 123000000.000
  • 0.1234567
  • 1.1234567

唯一的解决方案,我想出了到目前为止,我甚至不知道它是否真的有效,看起来是这样,但运行速度很慢,有没有更快的方法?

SELECT * 
FROM table 
WHERE LENGTH(TO_CHAR(ABS(field))) > 9; 
+1

我不认为有办法解决这个问题,因为没有办法检查一个数字并确定有多少数字实际上是重要的。当然,例如,'123000000.000'。为什么*那​​无效,但是'123000001.000'或'122999999.000'会有效?您不能自动确定尾随“0”(小数点前)不重要。 –

+0

浮点值以二进制形式存储。从字符表示转换到字符表达时,某些舍入问题可能会带来非常意想不到的结果。什么看起来像几个小数浮点数,被存储为类似的东西 - 但四舍五入,并返回许多小数。 – jarlh

回答

1

对于您的significance的具体定义,我认为没有更好的方法。甚至更多您提出的解决方案是不正确的,它会被误认为123000000.00甚至123456.7。所以,你需要删除.和尾随零:

with data (a) as 
(
    select 12345678 from dual union all 
    select 12345678.0 from dual union all 
    select 12345678.1 from dual union all 
    select 1234567 from dual union all 
    select 123456789.234 from dual union all 
    select 0.123456789 from dual 
) 
select 
    * 
from data 
where 
    length(rtrim(replace(to_char(abs(a)), '.'), '0')) >= 8 

不幸的是我没有Oracle数据库进行测试。但我有MS SQL Server的,所以我把它移植和测试:

with data (a) as 
(
    select 12345678 union all 
    select 12345678.0 union all 
    select 12345678.1 union all 
    select 1234567 union all 
    select 123456789.234 union all 
    select 0.123456789 
) 
select 
    * 
from data 
where 
    len(rtrim(replace(replace(str(abs(a)), '.', ''), '0', ' '))) >= 8 

所不同的只是在甲骨文=> SQL服务器:

  • rtrim('..', '0') =>rtrim(replace('...', '0', ' '))
  • length =>len
+0

非常感谢,这对我来说是个诀窍! – saidaspen

相关问题