Cast one of the values in MySQL and perform division with the other?


For this, at first, use CAST(). Let us first create a table −

mysql> create table DemoTable1352
    -> (
    -> Value1 int,
    -> Value2 int
    -> );
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command−

mysql> insert into DemoTable1352 values(10,30);
Query OK, 1 row affected (0.42 sec)
mysql> insert into DemoTable1352 values(40,60);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1352 values(110,130);
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement−

mysql> select * from DemoTable1352;

This will produce the following output −

+--------+--------+
| Value1 | Value2 |
+--------+--------+
|     10 |     30 |
|     40 |     60 |
|    110 |    130 |
+--------+--------+
3 rows in set (0.00 sec)

Here is the query to cast one of the values and perform division −

mysql> select cast(Value1 as DECIMAL(18,14)) / Value2 from DemoTable1352;

This will produce the following output −

+-----------------------------------------+
| cast(Value1 as DECIMAL(18,14)) / Value2 |
+-----------------------------------------+
|                    0.333333333333333333 |
|                    0.666666666666666666 |
|                    0.846153846153846153 |
+-----------------------------------------+
3 rows in set (0.00 sec)
raja
Published on 05-Nov-2019 09:35:06
Advertisements