How about such an approach? Example is based on Scott's EMP
table. I'd like to fetch employees which were hired in certain months.
Sample data:
SQL> select deptno, ename, job, hiredate, to_char(hiredate, 'mm') mon from emp order by mon;
DEPTNO ENAME JOB HIREDATE MO
---------- ---------- --------- ---------- --
20 ADAMS CLERK 12.01.1983 01
10 MILLER CLERK 23.01.1982 01
30 ALLEN SALESMAN 20.02.1981 02 -- suppose I want to select employees
30 WARD SALESMAN 22.02.1981 02 -- hired in February,
20 JONES MANAGER 02.04.1981 04 -- April and
30 BLAKE MANAGER 01.05.1981 05 -- May
10 CLARK MANAGER 09.06.1981 06
30 TURNER SALESMAN 08.09.1981 09
30 MARTIN SALESMAN 28.09.1981 09
10 KING PRESIDENT 17.11.1981 11
20 SCOTT ANALYST 09.12.1982 12
20 SMITH CLERK 17.12.1980 12
30 JAMES CLERK 03.12.1981 12
20 FORD ANALYST 03.12.1981 12
14 rows selected.
Query - which splits input search string into rows (so that you could use it in IN
clause) would then be:
SQL> with src as
2 (select &par_month src_month from dual)
3 select deptno, ename, job, hiredate
4 from emp
5 where to_number(to_char(hiredate, 'mm')) in
6 (select regexp_substr(src_month, '[^,]+', 1, level)
7 from src
8 connect by level <= regexp_count(src_month, ',') + 1
9 )
10 order by to_char(hiredate, 'mm');
Enter value for par_month: '2,4,5'
DEPTNO ENAME JOB HIREDATE
---------- ---------- --------- ----------
30 ALLEN SALESMAN 20.02.1981
30 WARD SALESMAN 22.02.1981
20 JONES MANAGER 02.04.1981
30 BLAKE MANAGER 01.05.1981
SQL>