简体   繁体   English

我如何修复此代码以使其修剪尾随零和小数点

[英]How would I fix this code to make it trim the trailing zeros and decimal point

I need to trim the trailing 0s and decimal point, I tried the following code and it wouldn't work. 我需要修剪尾随的0和小数点,我尝试了以下代码,它将无法正常工作。 I'm using PostgreSQL and the end result would look something like (from displaying 1234.00 to "1234 ft") 我正在使用PostgreSQL,最终结果看起来像(从显示1234.00到“1234英尺”)

SELECT TRIM(TRAILING "0" FROM ROUND("Length"::numeric * 3.28084, 2)) || ''ft'' AS "Length"

Error code I'm getting: 错误代码我得到:

ERROR:  column "Length" does not exist
LINE 1: SELECT TRIM(TRAILING '0' FROM ROUND("Length"::numeric * 3.28...
                                            ^                       ^

Try this: 尝试这个:

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

Test Result: 测试结果:

DB<>Fiddle DB <>小提琴

Update: 更新:

I just found an easier one 我刚刚发现了一个更容易的

select concat(cast(1234.00 as decimal(10,0)),' ft') as Length
from ...

Replace 1234.00 with your column name and add 'from' clause. 将1234.00替换为您的列名称并添加“from”子句。

I would just do: 我会这样做:

SELECT ("Length"::numeric * 3.28084)::int || 'ft' as Length

You can round() as well. 你也可以round() The point is to convert to an integer so you have no decimals. 重点是转换为整数,因此您没有小数。

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

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