Q

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?

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:

SELECT * FROM A,B
WHERE INSTR(a.col_1,b.col_2) > 0
AND SUBSTR(INSTR(a.col_1,b.col_2),1,1)=',')
AND
SUBSTR(INSTR(a.col_1,b.col_2)+LENGTH(a.col_1),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

Dig deeper on Oracle database design and architecture

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close