
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Extract Middle Part of Column Values in MySQL
Use the SUBSTR() method to extract the middle part of column values surrounded with hyphens, for example, “11-84848-11”.
Let us first create a table −
mysql> create table DemoTable -> ( -> Number varchar(100), -> Number1 varchar(100) -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Number) values('11-84848-11'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Number) values('22-99999-22'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-------------+---------+ | Number | Number1 | +-------------+---------+ | 11-84848-11 | NULL | | 22-99999-22 | NULL | +-------------+---------+ 2 rows in set (0.00 sec)
Following is the query to extract the middle part of column values in MySQL −
mysql> update DemoTable -> set Number1=SUBSTR(Number,4,5); Query OK, 2 rows affected (0.19 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
Output
This will produce the following output −
+-------------+---------+ | Number | Number1 | +-------------+---------+ | 11-84848-11 | 84848 | | 22-99999-22 | 99999 | +-------------+---------+ 2 rows in set (0.00 sec)
Advertisements