[英]How to get an accurate length of a long digit number in Oracle
I'm working with a schema (not created by me) that has calculated variances that can be really long numbers.我正在使用一个模式(不是由我创建的),它计算出的方差可能是非常长的数字。 I need to identify these long numbers so I can trunc them.我需要识别这些长数字,以便截断它们。 The problem is that they are so long, the length function doesn't seem to recognize them as being long.问题是它们太长了,长度 function 似乎并不认为它们很长。
SELECT Length(10.00000000000000000000000000000000000004) FROM dual; SELECT 长度(10.00000000000000000000000000000000000004)来自双;
result: 2结果:2
Obviously the number above is longer than 2 digits, but the length function doesn't recognize this.显然上面的数字长于 2 位,但是长度 function 无法识别这一点。 How do I get an accurate length of this number, so I can find all the cases where they exist?如何获得该数字的准确长度,以便找到它们存在的所有案例?
This happens because the length()
function accepts a string as input, not a number, so the value is implicitly converted to a string using some default format before the function ever sees it.发生这种情况是因为length()
function 接受字符串作为输入,而不是数字,因此在 function 看到它之前,使用某种默认格式将值隐式转换为字符串。
There may be a more elegant mathematical solution, but one approach would be to use the 'text minimum' number format , length(to_char(num,'TM'))
:可能有一个更优雅的数学解决方案,但一种方法是使用“文本最小” 数字格式, length(to_char(num,'TM'))
:
with demo(num) as (select 10.00000000000000000000000000000000000004 from dual)
select to_char(num)
, to_char(num,'TM')
, length(to_char(num,'TM'))
from demo;
TO_CHAR(NUM) TO_CHAR(NUM,'TM') LENGTH(TO_CHAR(NUM,'TM'))
------------- ----------------------------------------- -------------------------
10 10.00000000000000000000000000000000000004 41
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.