Query to get file extensions

Hi ,
   I have an Oracle 11g table  called MyFiles with MyFileNames
      The sample entries are
         file1.doc
         file2.docx
         file3.xxxx

     I am looking for a query to  get only the file extensions

Thanks
Sam OZAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please try this -

WITH CTE AS
(
    SELECT 'f1.doc' fname FROM DUAL UNION ALL
    SELECT 'file2.docx' fname FROM DUAL UNION ALL
    SELECT 'fl.xxxx' fname FROM DUAL UNION ALL
    SELECT 'testfile.abcxyz' fname FROM DUAL
)
SELECT REGEXP_SUBSTR(fname,'(\.)\S*') Extension from CTE 

Open in new window

0
Pawan KumarDatabase ExpertCommented:
or this -

WITH CTE AS
(
    SELECT 'f1.xlsx' fname FROM DUAL UNION ALL
    SELECT 'file2.doc' fname FROM DUAL UNION ALL
    SELECT 'fl.h' fname FROM DUAL UNION ALL
    SELECT 'testfile.abcxyz' fname FROM DUAL UNION ALL
    SELECT 't...est..file.abcxyz' fname FROM DUAL
    
)
SELECT substr( fname, -instr( reverse( fname ), '.', 1, 1 ) )  Extension from CTE

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pawan KumarDatabase ExpertCommented:
or this - There are many ways.. :)

WITH CTE AS
(
    SELECT 'f1.xlsx' fname FROM DUAL UNION ALL
    SELECT 'file2.doc' fname FROM DUAL UNION ALL
    SELECT 'fl.h' fname FROM DUAL UNION ALL
    SELECT 'testfile.abcxyz' fname FROM DUAL UNION ALL
    SELECT 't...est..file.abcxyz' fname FROM DUAL
    
)
SELECT SUBSTR(fname,INSTR(fname,'.',-1),LENGTH(fname)) Extension from CTE 

Open in new window

0
Pawan KumarDatabase ExpertCommented:
another one-
WITH CTE AS
(
    SELECT 'f1.xlsx' fname FROM DUAL UNION ALL
    SELECT 'file2.doc' fname FROM DUAL UNION ALL
    SELECT 'fl.h' fname FROM DUAL UNION ALL
    SELECT 'testfile.abcxyz' fname FROM DUAL UNION ALL
    SELECT 't...est..file.abcxyz' fname FROM DUAL
    
)
select substr(fname, -instr(reverse(fname), '.', 1, 1)) as file_extension
from CTE

Open in new window


OUTPUT

 	FILE_EXTENSION
1	.xlsx
2	.doc
3	.h
4	.abcxyz
5	.abcxyz

Open in new window

0
awking00Commented:
select substr(MyFileNames,- instr(MyFileNames,,'.')) from MyFiles
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.