db2:如何从数字列中删除小数?

db2: how to remove the decimal from number column?

一个非常简单的有效查询

select avg(price) from cars where type = 'electric';

结果是

1                                
---------------------------------
                45000,00000000000

我要删除,00000000000以获得

   1                                
    ---------------------------------
                    45000

我试过投射和舍入但没有成功。 我在 Db2

这个漂亮的 ltrim 适用于所有类型的汽车

select replace(ltrim(replace(price,'0',' ')),' ','0') from cars;

但与 avg return 一起使用带有 00000

的字符串
select replace(ltrim(replace(avg(price),'0',' ')),' ','0') from cars where type = 'electric';

结果是..

45000.00000000000

:( 只有 , 改变了。

cast这个值应该可以工作:

select cast(avg(price) as int) as avg_int
from cars 
where type = 'electric';

按照另一个答案的建议转换为整数将有效。

但是,请记住

  1. 您将截断任何小数值。
  2. 您正在从一种类型转换为另一种类型。

您可以通过在 ROUND()

之后投射来解决 #1
select int(round(avg(price),0)) as avg_int
from cars 
where type = 'electric'

您可以通过使用 DECIMAL()ZONED() 来解决 #2,根据原始列类型使用零作为标度。

select dec(avg(price),10,0) as avg_dec
from cars 
where type = 'electric'

当然 ROUND() 也可以与 DECIMAL()ZONED() 一起使用...