Remove Trailing Zero in MySQL?

MySQLMySQLi Database

Use trim() function to remove trailing zeroz in MySQL. Following is the syntax −

select trim(yourColumnName)+0 As anyAliasName from yourTableName;

Let us first create a table −

mysql> create table removeTrailingZero
   -> (
   -> Number DECIMAL(10,4)
   -> );
Query OK, 0 rows affected (0.83 sec)

Following is the query to insert some records in the table using insert command −

mysql> insert into removeTrailingZero values(10.789);
Query OK, 1 row affected (0.19 sec)

mysql> insert into removeTrailingZero values(89.90);
Query OK, 1 row affected (0.18 sec)

mysql> insert into removeTrailingZero values(8999.70);
Query OK, 1 row affected (0.20 sec)

mysql> insert into removeTrailingZero values(0.40);
Query OK, 1 row affected (0.23 sec)

mysql> insert into removeTrailingZero values(0.0);
Query OK, 1 row affected (0.16 sec)

Following is the query to display all records from the table using select statement −

mysql> select * from removeTrailingZero;

This will produce the following output −

+-----------+
| Number    |
+-----------+
| 10.7890   |
| 89.9000   |
| 8999.7000 |
| 0.4000    |
| 0.0000    |
+-----------+
5 rows in set (0.00 sec)

Following is the query to remove trailing zeros −

mysql> select trim(Number)+0 As WithoutTrailingZero from removeTrailingZero;

This will produce the following output −

+---------------------+
| WithoutTrailingZero |
+---------------------+
| 10.789              |
| 89.9                |
| 8999.7              |
| 0.4                 |
| 0                   |
+---------------------+
5 rows in set (0.00 sec
raja
Published on 16-Apr-2019 13:19:56
Advertisements