- 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
Extracting filenames from a path in MySQL?
To extract filenames from a path MySQL, you can use SUBSTRING_INDEX(). The syntax is as follows −
SELECT SUBSTRING_INDEX(ypurColumnName, '\', -1) as anyAliasName FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table ExtractFileNameDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> AllProgrammingFilePath varchar(100) -> ); Query OK, 0 rows affected (0.50 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into ExtractFileNameDemo(AllProgrammingFilePath) values('C:\Users\John\AddTwoNumberProgram.java'); Query OK, 1 row affected (0.13 sec) mysql> insert into ExtractFileNameDemo(AllProgrammingFilePath) values('E:\CProgram\MasterMindGame.c'); Query OK, 1 row affected (0.23 sec) mysql> insert into ExtractFileNameDemo(AllProgrammingFilePath) values('F:\WebApplication\WebApp.php'); Query OK, 1 row affected (0.19 sec) mysql> insert into ExtractFileNameDemo(AllProgrammingFilePath) values('C:\Users\John\Desktop\AllMySQLScript.sql'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from ExtractFileNameDemo;
The following is the output −
+----+------------------------------------------+ | Id | AllProgrammingFilePath | +----+------------------------------------------+ | 1 | C:\Users\John\AddTwoNumberProgram.java | | 2 | E:\CProgram\MasterMindGame.c | | 3 | F:\WebApplication\WebApp.php | | 4 | C:\Users\John\Desktop\AllMySQLScript.sql | +----+------------------------------------------+ 4 rows in set (0.00 sec)
Here is the query to extract file names from a path in MySQL −
mysql> select SUBSTRING_INDEX(AllProgrammingFilePath, '\', -1) as AllFileName from ExtractFileNameDemo;
The following is the output −
+--------------------------+ | AllFileName | +--------------------------+ | AddTwoNumberProgram.java | | MasterMindGame.c | | WebApp.php | | AllMySQLScript.sql | +--------------------------+ 4 rows in set (0.00 sec)
Advertisements