Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file extension from filename in PostgreSQL?

Using PostgreSQL 9.3. Have a column with file name values like that:

qwerty.swf
some.image.jpg
some.other.image.jpg 

This column also allow null values.

How to get file extensions from this column in sql query?

like image 864
kostepanych Avatar asked Jun 29 '16 14:06

kostepanych


People also ask

What is a PostgreSQL extension?

A useful extension to PostgreSQL typically includes multiple SQL objects; for example, a new data type will require new functions, new operators, and probably new index operator classes. It is helpful to collect all these objects into a single package to simplify database management. PostgreSQL calls such a package an extension.

How do I create an extension from a dump in PostgreSQL?

When pg_dump is used, the CREATE EXTENSION command will be included in the dump, followed by the set of GRANT and REVOKE statements necessary to set the privileges on the objects to what they were at the time the dump was taken. PostgreSQL does not currently support extension scripts issuing CREATE POLICY or SECURITY LABEL statements.

How to get file extension from a file name in Python?

The .split (".").pop () will return the last piece from the split file name. Hence it is a smart method to get file extension from a given filename. For extracting the filenames, we employ string operations as we are dealing with the file name string.

How do I define an extension in SQL Server?

To define an extension, you need at least a script file that contains the SQL commands to create the extension's objects, and a control file that specifies a few basic properties of the extension itself. If the extension includes C code, there will typically also be a shared library file into which the C code has been built.


1 Answers

try :

    with mytable as (
    select unnest(string_to_array($$qwerty.swf
    some.image.jpg
    some.other.image.jpg
    test
    $$,E'\n')) filename)

    select filename,substring(filename from '\.([^\.]*)$') 
    from mytable
like image 123
Rémy Baron Avatar answered Oct 05 '22 23:10

Rémy Baron