Can we replace all the digits of column values to zero except the first digit?


Yes, we can replace all the digits of column values to zero except the first digit. Let us first see an example and create a table −

mysql> create table DemoTable744 (Number varchar(100));
Query OK, 0 rows affected (0.47 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable744 values('537737736252');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable744 values('989000099999');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable744 values('343225666666664533');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable744 values('4322111899494');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable744;

This will produce the following output -

+--------------------+
| Number             |
+--------------------+
| 537737736252       |
| 989000099999       |
| 343225666666664533 |
| 4322111899494      |
+--------------------+
4 rows in set (0.00 sec)

Following is the query to replace all the digits of column values to zero except the first digit −

mysql> select CONCAT(LEFT(Number, 1), REPEAT('0', CHAR_LENGTH(Number) - 1)) 
   AS AfterAddingZero from DemoTable744;

This will produce the following output -

+--------------------+
| AfterAddingZero    |
+--------------------+
| 500000000000       |
| 900000000000       |
| 300000000000000000 |
| 4000000000000      |
+--------------------+
4 rows in set (0.06 sec)

Updated on: 22-Aug-2019

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements