- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements