Joining part of a field to another table

Posted on 2007-04-03
Last Modified: 2010-03-19
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

Question by:nkewney
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 50

Expert Comment

ID: 18847479
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...
LVL 50

Accepted Solution

Lowfatspread earned 500 total points
ID: 18847507
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 '%.' + + ' %'  
LVL 11

Expert Comment

ID: 18847514

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

I hope this can help you

Best Regards
LVL 11

Expert Comment

ID: 18847525
When i use Your code and use filename sth.gif.whatever.exe i can get wrong result

Correct me if im wrong.
LVL 11

Expert Comment

ID: 18847560
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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question