移除小数位后无意义的零,并且改变 MySQL 中的长度?
可以使用 TRIM() 函数移除尾随零。语法如下。
SELECT TRIM(yourColumnName)+0 FROM yourTableName;
为了理解上述的语法,让我们来创建一个表。创建表的查询如下 −
mysql> create table removeTrailingZeroInDecimal -> ( -> Id int not null auto_increment, -> Amount decimal(5,2), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (1.01 sec)
使用 insert 命令在表中插入一些记录。查询如下 −
mysql> insert into removeTrailingZeroInDecimal(Amount) values(405.50); Query OK, 1 row affected (0.22 sec) mysql> insert into removeTrailingZeroInDecimal(Amount) values(23.05); Query OK, 1 row affected (0.17 sec) mysql> insert into removeTrailingZeroInDecimal(Amount) values(12.050); Query OK, 1 row affected (0.14 sec) mysql> insert into removeTrailingZeroInDecimal(Amount) values(125.23); Query OK, 1 row affected (0.14 sec) mysql> insert into removeTrailingZeroInDecimal(Amount) values(125.00); Query OK, 1 row affected (0.15 sec) mysql> insert into removeTrailingZeroInDecimal(Amount) values(126); Query OK, 1 row affected (0.14 sec)
使用 select 语句从表中显示所有记录。查询如下 −
mysql> select *from removeTrailingZeroInDecimal;
输出如下。
+----+--------+ | Id | Amount | +----+--------+ | 1 | 405.50 | | 2 | 23.05 | | 3 | 12.05 | | 4 | 125.23 | | 5 | 125.00 | | 6 | 126.00 | +----+--------+ 6 rows in set (0.00 sec)
以下查询可以移除小数中的尾随零。查询如下 −
mysql> SELECT TRIM(Amount)+0 FROM removeTrailingZeroInDecimal;
输出显示了没有尾随零的记录。
+----------------+ | TRIM(Amount)+0 | +----------------+ | 405.5 | | 23.05 | | 12.05 | | 125.23 | | 125 | | 126 | +----------------+ 6 rows in set (0.00 sec)
广告