Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Get only the file extension from a column with file names as strings in MySQL?
For this, use the substring_index() function.
The syntax is as follows
select substring_index(yourColumnName, '. ', -1) AS anyAliasNamefrom yourTableName;
Let us first create a table. The query to create a table is as follows
mysql> create table AllFiles - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > UserName varchar(10), - > FileName varchar(100) - > ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into AllFiles(UserName,FileName) values('Larry','AddTwoNumber.java');
Query OK, 1 row affected (0.18 sec)
mysql> insert into AllFiles(UserName,FileName) values('Mike','AddTwoNumber.python');
Query OK, 1 row affected (0.15 sec)
mysql> insert into AllFiles(UserName,FileName) values('Sam','MatrixMultiplication.c');
Query OK, 1 row affected (0.16 sec)
mysql> insert into AllFiles(UserName,FileName) values('Carol','vector.cpp');
Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement.
The query is as follows
mysql> select *from AllFiles;
The following is the output
+----+----------+------------------------+ | Id | UserName | FileName | +----+----------+------------------------+ | 1 | Larry | AddTwoNumber.java | | 2 | Mike | AddTwoNumber.python | | 3 | Sam | MatrixMultiplication.c | | 4 | Carol | vector.cpp | +----+----------+------------------------+ 4 rows in set (0.00 sec)
Here is the query to get only the file extension in MySQL
mysql> select substring_index(FileName,'.',-1) AS ALLFILENAMEEXTENSIONS from AllFiles;
The following is the output
+-----------------------+ | ALLFILENAMEEXTENSIONS | +-----------------------+ | java | | python | | c | | cpp | +-----------------------+ 4 rows in set (0.00 sec)
Advertisements
