Computer >> Computer tutorials >  >> Programming >> MySQL

MySQL query to fetch records where decimal is a whole number


For this, use FLOOR() function. Here, we will be fetching records like 12.00, 35.00, etc. from a list with records like 5.23, 8.76, 12.00, 22.68, etc. Let us first create a table −

mysql> create table DemoTable
(
   Value DECIMAL(4,2)
);
Query OK, 0 rows affected (0.53 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(54.20);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(55.0);
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values(7.8);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(9.0);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(9.2);
Query OK, 1 row affected (0.21 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Value |
+-------+
| 54.20 |
| 55.00 |
|  7.80 |
|  9.00 |
|  9.20 |
+-------+
5 rows in set (0.00 sec)

Following is the query to fetch records where decimal is a whole number −

mysql> select *from DemoTable where Value=floor(Value);

This will produce the following output −

+-------+
| Value |
+-------+
| 55.00 |
|  9.00 |
+-------+
2 rows in set (0.02 sec)