Ask the Expert

Using a WHERE... IN clause in a comma delimited field

I'm using Oracle 8i. I have three tables: One table has a numeric column which contains one number. We'll call it A.Col_1. A second table has a varchar2 column which contains a comma delimited list of numbers. We'll call it B.Col_2. In my query, I need to do a WHERE IN clause WHERE A.Col_1 IN B.Col_2. But this does not work. How can I do this?

    Requires Free Membership to View

Since your second table has a list of comma delimited numbers, then you'll have to parse each number from that list to see if that column contains the number you are looking for. You'll have to do this using built SQL functions. The INSTR function will tell you where a string lies within another string. The SUBSTR function will return a substring of a string. So, I want to see if my column contains the entire number I am looking for and not just a portion of the number. For instance, if I am looking for the number '123', then I don't want a true result if the number is really '1234'. So something like the following is a start to your solution:

WHERE INSTR(a.col_1,b.col_2) > 0
AND SUBSTR(INSTR(a.col_1,b.col_2),1,1)=',')
If your SQL query starts to get too complicated, then you'll have to resort to parsing with PL/SQL.

For More Information

This was first published in March 2003

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: