Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get extension of a file using SQL? [duplicate]

Tags:

Possible Duplicate:
Get substring in SQL Server

Let's say I have MyImage.png or MyDoc.doc, etc in a column in a database table.

How can I only get the file extension?

like image 372
Imran Qadir Baksh - Baloch Avatar asked Nov 22 '12 08:11

Imran Qadir Baksh - Baloch


People also ask

How do I get the file extension in SQL?

In order to get file extension of file as a result of SQL query, you can use SUBSTRING_INDEX(). Insert some records in the table using insert command. Display all records from the table using select statement.

How do I remove filename extension in SQL Server?

From w3schools.com/sql/func_sqlserver_charindex.asp: "The position where the search will start (if you do not want to start at the beginning of string). The first position in string is 1." If you have Strings inside your column that have more than 999 chars you have to modify this parameter.

What file extension does SQL use?

SQL Server supports two types of format files: XML formats and non-XML format files. Both non-XML format files and XML format files contain descriptions of every field in a data file, and XML format files also contain descriptions of the corresponding table columns.

How do I change the extension of a file in SQL?

In SQL Server Management Studio, from the Tools menu, click Options. In the Options dialog box, click Text Editor, and then click File Extension. In the Extension box, type your new file extension.


1 Answers

try this:

declare @str varchar(20)='MyDoc.doc'; select reverse(left(reverse(@str),charindex('.',reverse(@str))-1)) 

SQL fiddle demo

like image 170
Joe G Joseph Avatar answered Oct 21 '22 02:10

Joe G Joseph