- 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
Fetch middle part of a string surrounded by slash in MySQL
Let us first create a table −
mysql> create table DemoTable -> ( -> Code varchar(100) -> ); Query OK, 0 rows affected (1.07 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('/101/102/106'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('/110/111/101'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('/111/114/201'); Query OK, 1 row affected (0.47 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------------+ | Code | +--------------+ | /101/102/106 | | /110/111/101 | | /111/114/201 | +--------------+ 3 rows in set (0.00 sec)
Here is the query to retrieve the middle string surrounded by slash −
mysql− select SUBSTRING_INDEX(SUBSTRING_INDEX(Code,'/',3),'/',-1) from DemoTable;
Output
This will produce the following output −
+-----------------------------------------------------+ | SUBSTRING_INDEX(SUBSTRING_INDEX(Code,'/',3),'/',-1) | +-----------------------------------------------------+ | 102 | | 111 | | 114 | +-----------------------------------------------------+ 3 rows in set (0.00 sec)
Advertisements