- 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
Select text after last slash in MySQL?
You need to use substring_index() function from MySQL to select text.
The syntax is as follows
SELECT substring_index(yourColumnName,'/',-1) AS anyAliasName FROM yourTableName;
To understand the above concept, let us create a table. The query to create a table is as follows
mysql> create table selectTextAfterLastSlashDemo - > ( - > UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > UserPathDirectory varchar(200) - > ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('C:/MyFolder1/MyEntityFramework'); Query OK, 1 row affected (0.18 sec) mysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('D:/MySpringFrameworkDemo'); Query OK, 1 row affected (0.14 sec) mysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('E:/Java/MyRootFolder/Source/AllHibernateDemo'); Query OK, 1 row affected (0.14 sec) mysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('C:/Program Files/MySQL/Server 8.0'); Query OK, 1 row affected (0.20 sec) mysql> insert into selectTextAfterLastSlashDemo(UserPathDirectory) values('C:/John/Folder1/Folder2'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from selectTextAfterLastSlashDemo;
The following is the output displaying path in of the columns
+--------+----------------------------------------------+ | UserId | UserPathDirectory | +--------+----------------------------------------------+ | 1 | C:/MyFolder1/MyEntityFramework | | 2 | D:/MySpringFrameworkDemo | | 3 | E:/Java/MyRootFolder/Source/AllHibernateDemo | | 4 | C:/Program Files/MySQL/Server 8.0 | | 5 | C:/John/Folder1/Folder2 | +--------+----------------------------------------------+ 5 rows in set (0.00 sec)
Here is the query to get text after last slash
mysql> select substring_index(UserPathDirectory,'/',-1) as TextAfterLastSlash from selectTextAfterLastSlashDemo;
The following is the output displaying the text after last slash
+-----------------------+ | TextAfterLastSlash | +-----------------------+ | MyEntityFramework | | MySpringFrameworkDemo | | AllHibernateDemo | | Server 8.0 | | Folder2 | +-----------------------+ 5 rows in set (0.00 sec)
Advertisements