首页 > 解决方案 > 更新没有小数点和零的数字字段

问题描述

我正在尝试更新数字字段。但该字段小数点后不能有零。但是我试图提取值的表包含 87.00、90.00、100.00 等数据。如何在没有小数点和零的情况下更新?

示例 :percentage 是一个数字字段。更新可用值 100.00,90.00 等。

update table1 
  set percent =(tmpercent as integer) 
from table2
where table2.custid=table1.custoid;

;

给出错误。

表格1:

CustID   Percent(numeric)
1           90
2           80

表2:

CustomID  tmpPercent(varchar)
1              87.00
2              90.00 

标签: postgresql

解决方案


我经常使用类型转换::FLOAT::NUMERIC来摆脱数字的额外小数零

或者您可以使用TRUNC()函数强制截断分数

尝试

update table1 
    set percent = tmpercent::FLOAT::NUMERIC 
from table2
where table2.custid=table1.custoid;

或者

update table1 
    set percent = TRUNC(tmpercent::NUMERIC)
from table2
where table2.custid=table1.custoid;

推荐阅读