Question : Return only rows that match

I would like to return all rows if an assignment_id  is not entered
If a value is entered, only return the rows that match the entered assignment_id value
With the following query construct, if an assignment_id  value is entered, it return the rows that matches the entered assignment_id  and also rows that have a null assignment_id. How can I construct the query to return only the rows that matches the entered value or if null or <ALL> is entered return all personnel.  We use a fuction to return a wildcard if <ALL> is entered.

SELECT DISTINCT  emp_id,
       p.emp_name,
       p.ssn,
       p.last_reviewed_date up_to_date,
       p.dept,
       cpp.assignment,
       cpp.assignment_id
 FROM   personnel_vw p
      INNER JOIN access_check_vw vw
        ON    p.dept_id  = vw.dept_id
         AND  p.dept like '%VEHICLE2%'
         AND  p.active = 'Y'
         AND (p.emp_status = 'PERMANENT' and p.assignment_type = 'SALES')  
         AND vw.pw_id = '111'
     INNER JOIN chk_per_unit_vw cpu
       ON p.emp_id = cpu.emp_id  
         AND cpu.unit_id  LIKE get_decode_null('<ALL>') -- Function -  If <ALL> decodes to %, if not, decodes to the entered value
   LEFT JOIN chk_per_assignments_vw cpp
    ON   p.emp_id = cpp.emp_id  
   AND nvl(cpp.assignment_id,'%') LIKE get_decode_all('10930') -- Function -  If <ALL> decodes to %, if not decodes to the entered value

Answer : Return only rows that match

I think I understand what you are after.  See if this helps.

It was all executed using SQL*Plus.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
drop table tab1 purge;
create table tab1(col1 varchar2(10));

insert into tab1 values('Hello');
insert into tab1 values('World');
commit;


var myVar varchar2(10)
--entered 'll'
exec :myVar := 'll';
select * from tab1 where col1 like '%' || decode(:myVar,'<ALL>',null,:myVar) || '%';

--entered null
exec :myVar := null;
select * from tab1 where col1 like '%' || decode(:myVar,'<ALL>',null,:myVar) || '%';


--entered '<ALL>'
exec :myVar := '<ALL>';
select * from tab1 where col1 like '%' || decode(:myVar,'<ALL>',null,:myVar) || '%';
Random Solutions  
 
programming4us programming4us