- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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)
- Related Articles
- How to get file extension of file as a result of MySQL query?
- Change the file extension in the text column in MySQL?
- Get file extension name in Java
- How to get left substring in MySQL from a column with file path? Display the entire file path string excluding the file name?
- How to get the file extension using PowerShell?
- C# program to get the extension of a file in C#
- How to save matrix created in R as tables in a text file with column names same as the matrix?
- Perl File Extension
- Create temporary file with specified extension suffix in Java
- How to get substring results from a table with file location recordsi in MySQL?
- How to change file extension in Python?
- How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?
- Setting column values as column names in the MySQL query result?
- Insert a word before the file name’s dot extension in JavaScript?
- How to extract file extension using Python?

Advertisements