- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 substrings from a string with words separated by slash in MySQL?
For this, you can use SUBSTRING_INDEX(). Let us first create a −
mysql> create table DemoTable1416 -> ( -> StudentCode varchar(100) -> ); Query OK, 0 rows affected (1.56 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1416 values('101/John/Smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1416 values('901/Carol/Taylor'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1416 values('400/David/Miller'); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select −
mysql> select * from DemoTable1416;
This will produce the following output −
+------------------+ | StudentCode | +------------------+ | 101/John/Smith | | 901/Carol/Taylor | | 400/David/Miller | +------------------+ 3 rows in set (0.00 sec)
Following is the query to fetch substrings from a string with words separated by slash −
mysql> select substring_index(StudentCode,'/',-2) from DemoTable1416;
This will produce the following output −
+-------------------------------------+ | substring_index(StudentCode,'/',-2) | +-------------------------------------+ | John/Smith | | Carol/Taylor | | David/Miller | +-------------------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Fetch middle part of a string surrounded by slash in MySQL
- How to use MySQL SELECT LEFT to fetch the records containing string with slash
- Fetch records from comma separated values using MySQL IN()?
- Fetch some words from the left in MySQL
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
- How to fetch random rows in MySQL with comma separated values?
- How to sum a comma separated string (string with numbers) in MySQL?
- Remove specific word in a comma separated string with MySQL
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- How to replace backward "" slash from a string
- MySQL RegExp to fetch records with only a specific number of words
- MySQL query to fetch specific records matched from an array (comma separated values)
- Parse a string to get a number from a large string separated by underscore
- Fetch random rows from a table with MySQL
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L) in C++

Advertisements