Delete last value and fix two new values (VARCHAR Numbers) in MySQL declared as VARCHAR?


Let us first create a table. Here, we have VARCHAR type for value −

mysql> create table DemoTable
(
   Value varchar(100)
);
Query OK, 0 rows affected (1.80 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('100');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values('1244');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values('15789');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------+
| Value |
+-------+
| 100   |
| 1244  |
| 15789 |
+-------+
3 rows in set (0.07 sec)

Here is the query to delete the last value and add new two varchar number values in MySQL. We have set the value 55 after deleting the last value −

mysql> update DemoTable set Value=concat(substring(Value,1,length(Value)-1), '55');
Query OK, 3 rows affected (0.23 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Let us check the table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+--------+
| Value  |
+--------+
| 1055   |
| 12455  |
| 157855 |
+--------+
3 rows in set (0.00 sec)

Updated on: 30-Sep-2019

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements