To keep only first two characters and delete rest of the characters, use SUBSTRING().
Let us first create a table −
mysql> create table DemoTable743 (SubjectName varchar(100)); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable743 values('MySQL'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable743 values('Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable743 values('MongoDB'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable743 values('Python'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable743 values('Data Structure'); Query OK, 1 row affected (0.68 sec) mysql> insert into DemoTable743 values('Algorithm'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable743;
This will produce the following output -
+----------------+ | SubjectName | +----------------+ | MySQL | | Java | | MongoDB | | Python | | Data Structure | | Algorithm | +----------------+ 6 rows in set (0.00 sec)
Following is the query to keep first 2 characters in column value and delete rest of the characters−
mysql> update DemoTable743 set SubjectName=SUBSTRING(SubjectName,1,2); Query OK, 6 rows affected (0.12 sec) Rows matched: 6 Changed: 6 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable743;
This will produce the following output -
+-------------+ | SubjectName | +-------------+ | My | | Ja | | Mo | | Py | | Da | | Al | +-------------+ 6 rows in set (0.00 sec)