MySQL query to replace part of string before dot

MySQLMySQLi Database

For this, use CONCAT() along with SUBSTRING_INDEX(). Let us first create a −

mysql> create table DemoTable1424
   -> (
   -> Value varchar(60)
   -> );
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1424 values('567.78483733');
Query OK, 1 row affected (0.78 sec)
mysql> insert into DemoTable1424 values('1023.45252443');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1424 values('7893322.5635543434');
Query OK, 1 row affected (0.35 sec)
mysql> insert into DemoTable1424 values('90944.665665');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select −

mysql> select * from DemoTable1424;

This will produce the following output −

+--------------------+
| Value              |
+--------------------+
| 567.78483733       |
| 1023.45252443      |
| 7893322.5635543434 |
| 90944.665665       |
+--------------------+
4 rows in set (0.00 sec)

Following is the query to replace part of string before dot −

mysql> update DemoTable1424
   -> set Value=concat('10000.',substring_index(Value ,'.',-1));
Query OK, 4 rows affected (0.13 sec)
Rows matched: 4  Changed: 4 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1424;

This will produce the following output −

+------------------+
| Value            |
+------------------+
| 10000.78483733   |
| 10000.45252443   |
| 10000.5635543434 |
| 10000.665665     |
+------------------+
4 rows in set (0.00 sec)
raja
Published on 12-Nov-2019 05:44:02
Advertisements