
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Remove trailing zeros in decimal value with changing length in MySQL?
You can remove trailing zeros using TRIM() function. The syntax is as follows.
SELECT TRIM(yourColumnName)+0 FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
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 some records in the table using insert command. The query is as follows −
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)
Display all records from the table using select statement. The query is as follows −
mysql> select *from removeTrailingZeroInDecimal;
The following is the output.
+----+--------+ | 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)
Here is the query to remove trailing zeros in decimal value. The query is as follows −
mysql> SELECT TRIM(Amount)+0 FROM removeTrailingZeroInDecimal;
The output displays the records without the trailing zeros.
+----------------+ | TRIM(Amount)+0 | +----------------+ | 405.5 | | 23.05 | | 12.05 | | 125.23 | | 125 | | 126 | +----------------+ 6 rows in set (0.00 sec)
- Related Articles
- Remove Trailing Zeros from string in C++
- Remove Trailing Zero in MySQL?
- What is the effect of trailing zeros on the decimal numbers?
- MySQL query to remove trailing spaces
- How to remove leading and trailing whitespace from a MySQL field value?
- Truncate results in decimal to integer value with MySQL
- How to remove leading and trailing whitespace in a MySQL field?
- Update a column in MySQL and remove the trailing underscore values
- Count trailing zeros in factorial of a number in C++
- How to remove white spaces (leading and trailing) from string value in MongoDB?
- Count number of trailing zeros in product of array in C++
- Remove trailing numbers surrounded by parenthesis from a MySQL column
- Add trailing Zeros to a Python string
- Finding trailing zeros of a factorial JavaScript
- Program to find trailing zeros in factorial of n in C++?\n

Advertisements