
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get left substring in MySQL from a column with file path? Display the entire file path string excluding the file name?
To get the left substring, use LEFT() along with substring_index(). For example, letβs say the file path is β
β/MyFile/JavaProgram/Hello.java β
Here, we will see how to display the entire file path except for the file name i.e. β
/MyFile/JavaProgram/
Let us first create a table β
mysql> create table DemoTable ( FileLocation text ); Query OK, 0 rows affected (0.57 sec
Insert some records in the table using insert command β
mysql> insert into DemoTable values('/MyFile/JavaProgram/Hello.java'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('/C/AllPrograms/animation.gif'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('/E/FavFile/ChatProgram.java'); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement β
mysql> select *from DemoTable;
This will produce the following output β
+--------------------------------+ | FileLocation | +--------------------------------+ | /MyFile/JavaProgram/Hello.java | | /C/AllPrograms/animation.gif | | /E/FavFile/ChatProgram.java | +--------------------------------+ 3 rows in set (0.00 sec)
Following is the query to get left substring in MySQL β
mysql> select left(FileLocation,char_length(FileLocation)-char_length(substring_index(FileLocation,'/',-1))) from DemoTable;
This will produce the following output β
+------------------------------------------------------------------------------------------------+ | left(FileLocation,char_length(FileLocation)-char_length(substring_index(FileLocation,'/',-1))) | +------------------------------------------------------------------------------------------------+ | /MyFile/JavaProgram/ | | /C/AllPrograms/ | | /E/FavFile/ | +------------------------------------------------------------------------------------------------+ 3 rows in set (0.00 sec)
Advertisements