我需要修剪尾随的0和小数点,我尝试了以下代码,它将无法正常工作。我正在使用PostgreSQL,最终结果看起来像(从显示1234.00到“1234英尺”)
SELECT TRIM(TRAILING "0" FROM ROUND("Length"::numeric * 3.28084, 2)) || ''ft'' AS "Length"
错误代码我得到:
ERROR: column "Length" does not exist
LINE 1: SELECT TRIM(TRAILING '0' FROM ROUND("Length"::numeric * 3.28...
^ ^
试试这个:
select concat(substring(cast(cast(1234.00 as decimal(10,2)) as varchar(50)),1,
position('.' in cast(cast(1234.00 as decimal(10,2)) as varchar(50)))-1),' ft')
as Length
测试结果:
更新:
我刚刚发现了一个更容易的
select concat(cast(1234.00 as decimal(10,0)),' ft') as Length
from ...
将1234.00替换为您的列名称并添加“from”子句。
我会这样做:
SELECT ("Length"::numeric * 3.28084)::int || 'ft' as Length
你也可以round()
。重点是转换为整数,因此您没有小数。