0

Is it possible to use LIKE inside an IN? The following query produces an error.

    Select * 
    from employees
    where salary between 2500 and 10000
    and job_id NOT in (like '%MAN%', like '%CLERK%');

What is the correct syntax? I'm using Oracle 11g

Ben
  • 51,770
  • 36
  • 127
  • 149
  • 1
    This *in like* thingy is a feature I wanted so many times. Oracle doesn't have such a thing. – René Nyffenegger Feb 15 '14 at 08:57
  • @RenéNyffenegger: I don't think any database has something like that. –  Feb 15 '14 at 09:05
  • @a_horse_with_no_name: Teradata has support for **LIKE ALL|ANY ('%MAN%','%CLERK%')**. ALL -> ANDed LIKEs, ANY -> ORed LIKEs, you might even use a SELECT to get the search strings – dnoeth Feb 15 '14 at 12:43
  • @dnoeth PostgreSQL has a similar feature with arrays: `LIKE ANY ARRAY ('%MAN%', %CLERK%')` – Mureinik Feb 15 '14 at 17:05
  • You look for `like any (...)` construct which does not work in Oracle but you can mimic it with `exists` and concatenated like expression, see my answer under linked question. (You can look also at the Sokrates` solution at AskTom.) – Tomáš Záluský Jun 23 '17 at 16:32

2 Answers2

4

You can't do both 'like' and 'in' at the time, but you can do this

    Select * 
      from employees
     where salary between 2500 
       and 10000
       and job_id NOT like '%MAN%' 
       and job_id NOT like '%CLERK%';
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
Elfentech
  • 747
  • 5
  • 10
3

Unfortunately, there is no "in like" operator. You could break it up to a series of LIKE conditions like @Elfentech suggested, or you could emulate it with the REGEXP_LIKE function:

SELECT *
FROM   employees
WHERE  salary BETWEEN 2500 AND 10000
AND    NOT REGEXP_LIKE (job_id, '.*[MAN|CLERK].*')

Alternatively, you could join on a bunch of UNION ALL queries from dual:

SELECT *
FROM  employees
JOIN  (SELECT 'MAN' AS search FROM dual 
       UNION ALL
       SELECT 'CLERK' FROM dual) jobs 
ON    employee.jod_id LIKE CONCAT ('%', jobs.search, '%')
WHERE salary BETWEEN 2500 AND 10000
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
Mureinik
  • 297,002
  • 52
  • 306
  • 350