Select rows having more than 2 decimal places in MySQL?

MySQLMySQLi Database

To select rows with more than 2 decimal places, use SUBSTR() function from MySQL. Let us first create a table −

mysql> create table selectRows2DecimalPlacesDemo
   -> (
   -> Amount varchar(100)
   -> );
Query OK, 0 rows affected (0.73 sec)

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

mysql> insert into selectRows2DecimalPlacesDemo values('234.5678');
Query OK, 1 row affected (0.17 sec)
mysql> insert into selectRows2DecimalPlacesDemo values('19.50');
Query OK, 1 row affected (0.19 sec)
mysql> insert into selectRows2DecimalPlacesDemo values('23.456');
Query OK, 1 row affected (0.17 sec)
mysql> insert into selectRows2DecimalPlacesDemo values('12.123');
Query OK, 1 row affected (0.14 sec)

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

mysql> select * from selectRows2DecimalPlacesDemo;

This will produce the following output −

+----------+
| Amount   |
+----------+
| 234.5678 |
| 19.50    |
| 23.456   |
| 12.123   |
+----------+
4 rows in set (0.00 sec)

Here is the query to select rows with more than two decimal places −

mysql> select *from selectRows2DecimalPlacesDemo WHERE
LENGTH(SUBSTR(Amount,INSTR(Amount,"."))) >3;

This will produce the following output −

+----------+
| Amount   |
+----------+
| 234.5678 |
| 23.456   |
| 12.123   |
+----------+
3 rows in set (0.00 sec)
raja
Published on 16-Apr-2019 13:17:07
Advertisements