MySQL中是否有内置函数删除右侧的尾随零?
我有样本,我希望我的输出是这样的:
1.0 ==> 1
1.50 ==> 1.5
10.030 ==> 10.03
0.50 ==> 0.5
0.0 ==> 0
答案 0 :(得分:9)
到目前为止最简单的方法,只需添加零!
示例:
SET
@yournumber1="1.0",
@yournumber2="1.50",
@yournumber3="10.030",
@yournumber4="0.50",
@yournumber5="0.0"
;
SELECT
(@yournumber1+0),
(@yournumber2+0),
(@yournumber3+0),
(@yournumber4+0),
(@yournumber5+0)
;
+------------------+------------------+------------------+------------------+------------------+
| (@yournumber1+0) | (@yournumber2+0) | (@yournumber3+0) | (@yournumber4+0) | (@yournumber5+0) |
+------------------+------------------+------------------+------------------+------------------+
| 1 | 1.5 | 10.03 | 0.5 | 0 |
+------------------+------------------+------------------+------------------+------------------+
1 row in set (0.00 sec)
如果您的值来自的列是DECIMAL或NUMERIC类型,则首先将其强制转换为字符串,以确保转换发生...例如:
SELECT (CAST(`column_name` AS CHAR)+0) FROM `table_name`;
对于更短的方法,只需使用任何内置字符串函数进行强制转换:
SELECT TRIM(`column_name`)+0 FROM `table_name`;
答案 1 :(得分:7)
它解决了我的问题:
(TRIM(TRAILING '.' FROM(CAST(TRIM(TRAILING '0' FROM setpoint)AS char)))) AS setpoint
示例:
mysql> SELECT testid, designationid, test, measure,
(TRIM(TRAILING '.' FROM(CAST(TRIM(TRAILING '0' FROM setpoint)AS char)))) AS setpoint,
(TRIM(TRAILING '.' FROM(CAST(TRIM(TRAILING '0' FROM tmin)AS char)))) AS tmin,
(TRIM(TRAILING '.' FROM(CAST(TRIM(TRAILING '0' FROM tmax)AS char)))) AS tmax,
FROM tests
答案 2 :(得分:1)
这是我的方法
SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM `table`.`column`)) FROM table
答案 3 :(得分:-1)
在我无法修改代码和SQL查询的情况下,我遇到了类似的问题,但我被允许修改数据库结构。所以我将列格式从DECIMAL更改为FLOAT,它解决了我的问题。