There is a Documents table with a DOC_FNAME field in which the full names of documents with their type are stored.

For example:

 +---------------+ |abrahadabra.pdf| |---------------| |some.txt | |---------------| |Doc.docx | +---------------+ 

You need a query to get the document type so that it is:

 +------+ |.pdf | |------| |.txt | |------| |.docx | +------+ 

I try this:

 SELECT ( SELECT (substr( d.DOC_FNAME, length(d.DOC_FNAME) - instr('.', d.DOC_FNAME)+1)) FROM dual) AS doctype FROM Documents d GROUP BY d.DOC_FNAME; 

but I do not get the desired result. Please help.

  • try instead of ( SELECT (substr( d.DOC_FNAME, length(d.DOC_FNAME) - instr('.', d.DOC_FNAME)+1)) FROM dual) just the condition substr(d.DOC_FNAME, instr('.', d.DOC_FNAME)) - Chubatiy
  • tried it. returns the full file name - LocalUser
  • I was wrong. Try substr(d.DOC_FNAME, instr(d.DOC_FNAME, '.')) - Chubatiy

1 answer 1

 select substr(DOC_FNAME,-instr(reverse(DOC_FNAME),'.')) from table 

Pay attention to the reverse function, it expands the line backwards to get the position of the last point, because there can be several points in modern filenames. The substr function is able to cut off from the end of the line; for this, a negative position is used.

Option 2, using regular expressions (for Oracle 10g +):

 select regexp_substr(DOC_FNAME,'\.[^.]*$') from table 
  • Sorry, really confused the tags. I use Oracle - LocalUser
  • @LocalUser I corrected the answer for Oracle ... - Mike
  • Your option returns returns the full file name, does not truncate to type. - LocalUser
  • @LocalUser Did you forget a minus before instr by chance? I checked on my Oracle, everything works fine. - Mike
  • strange. with minus and without - the same result is returned. I'll figure out why - LocalUser