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)

Updated on: 31-Dec-2019

620 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements