
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- MySQL SELECT last few days?
- How to select last row in MySQL?
- Select last day of current year in MySQL?
- How to select last two rows in MySQL?
- SELECT last entry without using LIMIT in MySQL?
- Select last record and update it in MySQL?
- Fetch the substring after last dot in MySQL
- How to use MySQL SELECT LEFT to fetch the records containing string with slash
- Select last 20 records ordered in ascending order in MySQL?
- Split a URL in JavaScript after every forward slash?
- How to select last 10 rows from MySQL?
- Select entries with timestamp after X time in MySQL
- Select date from MySQL and format to text?
- How to select between/before/after dates in MySQL conditionally?
- Select words from a text already in a MySQL table

Advertisements