简体   繁体   English

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

[英]db2: how to remove the decimal from number column?

A very simple query which work一个非常简单的查询

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

the result is结果是

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

I want to remove the ,00000000000 to obtain我想删除 ,00000000000 以获得

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

I have tried cast and round but without success.我尝试过投射和圆形但没有成功。 I'm on Db2我在 Db2 上

This nice ltrim works fine for all type of cars这个不错的 ltrim 适用于所有类型的汽车

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

but using with avg return the string with the 00000但是使用 with avg 返回带有 00000 的字符串

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

the result is..结果是..

45000.00000000000

:( Only the , is changed. :( 只有 , 被改变了。

cast ing this value should just work: cast荷兰国际集团这个值应该只是工作:

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

Casting as an integer as suggested by another answer will work.正如另一个答案所建议的那样,将其转换为整数将起作用。

However, keep in mind that但是,请记住,

  1. you'll be truncating any decimal values.您将截断任何十进制值。
  2. you're converting from one type to another.您正在从一种类型转换为另一种类型。

You can resolve #1 by casting after ROUND()您可以通过在ROUND()之后进行转换来解决 #1

select int(round(avg(price),0)) as avg_int
from cars 
where type = 'electric'

You can resolve #2 by using DECIMAL() or ZONED() with zero for scale depending on the original column type.您可以通过使用DECIMAL()ZONED()来解决 #2,根据原始列类型的比例为零。

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

And of course ROUND() could be used with DECIMAL() or ZONED() also...当然ROUND()也可以与DECIMAL()ZONED() ......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM