1

I have columnA of data type VARCHAR2(4000 BYTE) in an Oracle database tableA. columnA has comma-separated values as shown below (shows one record of the columnA for id=1).

one record

I need to insert the record as separate list of values into an IN clause using a sub select query.

Below is what I tried.

... IN (SELECT tableA.columnA FROM tableA WHERE tableA.id = '1')

Expected:

... IN (2222222226,2222222224,2222222227)

But I don't get the expected result. Please suggest a way for this.

2
  • 1
    A comma separated string is not the same as separate values in in clause. You're essentially doing ... IN ('2222222226,2222222224,2222222227') Commented Dec 6, 2022 at 16:23
  • You will need to parse the string and split it into multiple values - which you can do in your subquery. There are lots of questions about how to tokenize a comma-separated string. Commented Dec 6, 2022 at 16:25

1 Answer 1

3

You can also try:

FROM other_table
WHERE columnA IN (
    SELECT regexp_substr(columnA, '[^,]+', 1, LEVEL)
    FROM tableA
    CONNECT BY regexp_substr(columnA, '[^,]+', 1, LEVEL) IS NOT NULL
)

Here in SQLFiddle.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.