- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to update a value with substring of current value by removing the separator and numbers after a separator in MySQL?
Here, let’s say you have a string with form “StringSeparatorNumber” form like John/56989. Now if you want to remove the number after separator /, then use the SUBSTRING_INDEX(). Let us first create a table −
mysql> create table DemoTable ( StudentName varchar(100) ); Query OK, 0 rows affected (1.05 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John/56989'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('David/74674'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Bob/45565'); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+ | StudentName | +-------------+ | John/56989 | | Carol | | David/74674 | | Bob/45565 | +-------------+ 4 rows in set (0.00 sec)
Following is the query to update a value with substring of current value −
mysql> update DemoTable set StudentName=substring_index(StudentName,'/',1); Query OK, 3 rows affected (0.13 sec) Rows matched :4 Changed :3 Warnings :0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+ | StudentName | +-------------+ | John | | Carol | | David | | Bob | +-------------+ 4 rows in set (0.00 sec)
Advertisements