Joining part of a field to another table

Dear Experts

I have two tables

Document_Filename (i.e. nick.gif)


How can I extract the extension description from FileTypes (FileType_Description) from Document_Filename (where available)

I'm looking for the following result from my query:

Document_Filename  |  TypeOfFile
nick.gif                        CompuServe GIF

Any help would be appreciated

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.

which database system the sql syntax varies...
obviously you should hold the filetype as a distinct column in your table and then this would be unnecessary...
you could try ,..

select document_filename
    ,case when filetype_description is null then 'Unknown fileType'
                 else filetype_description end as typeoffile
 from documents as d
 left outer join filetypes as ft
  on d.document_filename + ' ' like '%.' + + ' %'  

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

You can use this function to extract file extension:
It returns substring after the last . (dot)

I hope this can help you

Best Regards
When i use Your code and use filename sth.gif.whatever.exe i can get wrong result

Correct me if im wrong.
If you are sure there is always just one '.' in the Document_Filename, you could use sqls substr and charindex functions.

SELECT SUBSTR(Document_Filename, charindex('.', Document_Filename))  from documents

should give  you the extension. If it has the . with it, you have to add a + 1 after the charindex.

The join could probably be done like:
SELECT documents.Document_Filename, FileTypes.FileType_Description from
documents left join FileTypes on FileTypes.FileType_Name = SUBSTR(Document_Filename, charindex('.', Document_Filename))

not sure, but give it a try
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
Microsoft SQL Server 2005

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.