
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Split Float Value in Two Columns of a MySQL Table
To split float value in two columns, the first column will have a value before decimal. The second column will have a value after decimal. For this, you can use SUBSTRING_INDEX() along with CAST(). Let us first create a table −
mysql> create table DemoTable1951 ( Value1 varchar(20) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1951 values('100.50'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1951 values('70.90'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1951 values('1000.55'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1951;
This will produce the following output −
+---------+ | Value1 | +---------+ | 100.50 | | 70.90 | | 1000.55 | +---------+ 3 rows in set (0.00 sec)
Here is the query to split float value in two columns:
mysql> select cast(substring_index(Value1, '.', 1) as unsigned) as FirstValue, cast(substring_index(Value1, '.', -1) as unsigned) as SecondValue from DemoTable1951;
This will produce the following output −
+------------+-------------+ | FirstValue | SecondValue | +------------+-------------+ | 100 | 50 | | 70 | 90 | | 1000 | 55 | +------------+-------------+ 3 rows in set (0.00 sec)
Advertisements