Dat*_*Dev 2 postgresql query decimal
如何计算所有小数位直到第一个零
例如,我有一个名为 x 的列,其中包含一个数值数据类型值 1.955000000
我想计算没有零的小数位。所以在这里我希望查询输出 3 而不是 9(带零)
我唯一能想到的是将值转换为字符串,删除所有尾随零,将其转换回数字,然后使用该scale()
函数:
scale(trim(trailing '0' from the_column::text)::numeric)
Run Code Online (Sandbox Code Playgroud)
下面的例子:
scale(trim(trailing '0' from the_column::text)::numeric)
Run Code Online (Sandbox Code Playgroud)
返回:
create table t1 (val numeric);
insert into t1
values
(1.955000000),
(1.10),
(1.1010),
(1.102030);
select val, scale(trim(trailing '0' from val::text)::numeric)
from t1;
Run Code Online (Sandbox Code Playgroud)