Skip to content
Advertisement

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

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

I have tried cast and round but without success. I’m on Db2

This nice ltrim works fine for all type of cars

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

but using with avg return the string with the 00000

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

the result is..

45000.00000000000

πŸ™ Only the , is changed.

Advertisement

Answer

casting this value should just work:

select cast(avg(price) as int) as avg_int
from cars 
where type = 'electric';
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement