We help IT Professionals succeed at work.

How do I pass a comma separated value from another table in an IN clause of a query in oracle.

smahaj2
smahaj2 asked
on
1,281 Views
Last Modified: 2013-12-18
I have two tables - table BATCH_CONTROL which has an Id and parameter like follows -
ID  prog_name  prog_value
1  EFT      A1,A2,A3,A4

I have another table EFT_PROTESTS like this -
ID  Job_Name ......  REASON_CD
01  batch1.........A1
02  batch2.........A2
03  batch3.........A5

I need to select all those rows from EFT_PROTESTS table where REASON_CD is present in BATCH_CONTROL table for Prog_name = 'EFT'
I tried to use this but does not work -
SELECT * from EFT_PROTESTS  eftprot where eftprot.reason_cd IN
       (select b.prog_value from BATCH_CONTROL b where b.prog_name = 'EFTPROT')

I am using ORACLE 10g.

Thanks..

Comment
Watch Question

It didnt work bcoz ID in EFT_PROTESTS table is padded with a leading 0. Try this.

SELECT * from EFT_PROTESTS  eftprot where LPAD(eftprot.reason_cd, 2, '0') IN
(select b.prog_value from BATCH_CONTROL b where b.prog_name = 'EFT')

Author

Commented:
Thank you for your prompt reply. I tried to LPAD it but the query does not return any results. It still shows no rows returned. it should have returned two rows as reason codes A1 and A2 are in the comma separated list in  batch_control table.
ID  Job_Name ......  REASON_CD
01  batch1.........A1
02  batch2.........A2



This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you so much Jinesh. That was of great help!!
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
INSTR(bc.prog_value, ep.reason_cd) != 0

will work provided none of your reason_cd values are substrings of each other
and none of your reason_cd values have the prog_value delimiter in them.

To be safe, you must check the exact value of each delimited element from batch_control.  


The only way I know to do that safely is with a function that splits the delimited string....

Here's an example....




CREATE OR REPLACE TYPE VCARRAY  AS TABLE OF VARCHAR2(4000);


CREATE OR REPLACE FUNCTION str2tbl(p_string IN VARCHAR2, p_delimiter IN VARCHAR2 := ',')
    RETURN vcarray PIPELINED
AS
    v_length   NUMBER := LENGTH(p_string);
    v_start    NUMBER := 1;
    v_index    NUMBER;
BEGIN
    WHILE(v_start <= v_length)
    LOOP
        v_index    := INSTR(p_string, p_delimiter, v_start);

        IF v_index = 0
        THEN
            PIPE ROW(SUBSTR(p_string, v_start));
            v_start    := v_length + 1;
        ELSE
            PIPE ROW(SUBSTR(p_string, v_start, v_index - v_start));
            v_start    := v_index + 1;
        END IF;
    END LOOP;

    RETURN;
END str2tbl;
/


select * from eft_protests,
where  reason_code in (select column_value from batch_control, table(str2tbl(prog_value,','))
Sean StuberDatabase Developer & Administrator
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2012

Commented:
sorry, I see was too late to help.

Hopefully your data is populated in such a way that the INSTR way won't give false positives.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.