MySQL - CAST DECIMAL to INT?

MySQLMySQLi Database

Cast DECIMAL to INT with the help of FLOOR() function. The syntax is as follows −

SELECT FLOOR(yourColumnName) from yourTableName where condition;

Let us first create a table. The following is the query to create a table.

mysql> create table DecimalToIntDemo
   -> (
   -> Amount DECIMAL(3,1)
   -> );
Query OK, 0 rows affected (0.88 sec)

Now you can insert records into the table with the help of insert command. The query is as follows −

mysql> insert into DecimalToIntDemo values(12.5);
Query OK, 1 row affected (0.23 sec)
mysql> insert into DecimalToIntDemo values(50.4);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DecimalToIntDemo values(48.6);
Query OK, 1 row affected (0.10 sec)

Display all records with the help of select statement. The query is as follows −

mysql> select *from DecimalToIntDemo;

Here is the output −

+--------+
| Amount |
+--------+
|   12.5 |
|   50.4 |
|   48.6 |
+--------+
3 rows in set (0.00 sec)

Apply the above syntax which we discussed in the beginning. The query is as follows −

mysql> SELECT FLOOR(Amount) from DecimalToIntDemo
   -> where Amount > 10;

The following is the output that cast decimal to int −

+---------------+
| FLOOR(Amount) |
+---------------+
|            12 |
|            50 |
|            48 |
+---------------+
3 rows in set (0.00 sec)

Look at the above sample which gives only INT value.

raja
Published on 24-Dec-2018 07:58:04
Advertisements