小数点前的数字为0时如何删除小数位

时间:2019-05-31 08:33:36

标签: sql sqlite

我正在尝试从某些值中删除小数位

例如,我当前的代码是

select po_id, AVG(price*quantity) * count(*) AS 'Total Cost'
from po_items natural join items 
group by po_id; 

这是即时通讯输出

po_id       Total Cost
----------  ----------
AAA         2217.5
BBB         255.0
CCC         5787.5
DDD         10000.0
GGG         1000.0

我试图获取的输出是

po_id       Total Cost
----------  ----------
AAA         2217.5
BBB         255
CCC         5787.5
DDD         10000
GGG         1000

2 个答案:

答案 0 :(得分:2)

确定,如下所示:

sqlite> select cost from my_table;
2217.5
255.0
5787.5
10000.0
1000.0

sqlite> select replace(cast(cost as string),'.0','') from my_table;
2217.5
255
5787.5
10000
1000

您可能会使用完整的SQL:

select 
    po_id, 
    replace(cast(avg(price*quantity)*count(1) as string),'.0','') as 'Total Cost'
from 
    po_items
join 
    items 
group by 
    po_id; 

答案 1 :(得分:0)

printf()%g格式说明符一起使用:

sqlite> select printf('%g', 5.0), printf('%g', 5.5);
printf('%g', 5.0)  printf('%g', 5.5)
-----------------  -----------------
5                  5.5