- 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
MySQL query to replace a string after the last / in a column with directory links?
For this, use the substring_index() method. Let us first create a table −
mysql> create table DemoTable -> ( -> FolderName varchar(100), -> FolderLocation varchar(200) -> ); Query OK, 0 rows affected (1.03 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('CProgram','C:/AllPrograms/.....'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Images','E:/MyImage/home/garbage'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+------------+-------------------------+ | FolderName | FolderLocation | +------------+-------------------------+ | CProgram | C:/AllPrograms/..... | | Images | E:/MyImage/home/garbage | +------------+-------------------------+ 2 rows in set (0.00 sec)
Here is the query to replace a string after the last / in a column with directory links −
mysql> UPDATE DemoTable SET FolderLocation = CONCAT(LEFT(FolderLocation , CHAR_LENGTH(FolderLocation ) - CHAR_LENGTH(SUBSTRING_INDEX(FolderLocation , '/', -1))),FolderName); Query OK, 2 rows affected (0.16 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
Output
+------------+-------------------------+ | FolderName | FolderLocation | +------------+-------------------------+ | CProgram | C:/AllPrograms/CProgram | | Images | E:/MyImage/home/Images | +------------+-------------------------+ 2 rows in set (0.00 sec)
Advertisements