1

I'm trying to get the position of a file extension in a given string using instr(). This could be a number of possible extensions, so I need search for multiple substrings (eg. "jpg", "mp4"...).

I've tried an OR statement, eg. instr(string,"jpg" OR "mp4"), with no luck.

SELECT instr(column, "jpg") FROM table;

Any ideas for solutions or alternatives would be welcome.

Ed Bangga
  • 12,879
  • 4
  • 16
  • 30

2 Answers2

2

This one is what you need.

SELECT CASE WHEN instr(column, "jpg") > 0 
   THEN instr(column, "jpg") WHEN instr(column, "mp4") > 0 
   THEN instr(column, "mp4") ELSE 0 END  FROM table;
Ed Bangga
  • 12,879
  • 4
  • 16
  • 30
0

Assuming that only one of 'jpg', 'mp4' or any other extension may exist in the column, then you can use this:

SELECT instr(column, "jpg") + instr(column, "mp4") + .... FROM table;

For all the non existing extensions instr() will return 0 so the result will be the starting position of the existing extension.
If you store all the extensions in a table with name extensions with a single column name, you could cross join it to your table and get the results:

select
  t.column, sum(instr(t.column, e.name)) position
from table t cross join extensions e
group by t.column 
forpas
  • 160,666
  • 10
  • 38
  • 76