For this, use SUBSTRING_INDEX(). Let us first create a table −
mysql> create table DemoTable2040 -> ( -> StudentCode varchar(20) -> ); Query OK, 0 rows affected (0.85 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2040 values('John-232'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2040 values('Carol-901'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2040 values('David-987'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2040;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | John-232 | | Carol-901 | | David-987 | +-------------+ 3 rows in set (0.00 sec)
Here is the query to remove numbers after hyphen −
mysql> update DemoTable2040 -> set StudentCode=substring_index(StudentCode,'-',1); Query OK, 3 rows affected (0.21 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable2040;
This will produce the following output −
+-------------+ | StudentCode | +-------------+ | John | | Carol | | David | +-------------+ 3 rows in set (0.00 sec)