2

I am stuck at one particular problem here, I am fetching ID's from one column that are like this ',90132988,90133148,72964884,' Let's say this value is stored in a column ColumnA of Table1.

I want to use this in another query that's using IN clause something like this.

SELECT * 
FROM USER_GROUP 
WHERE ID IN (SELECT CONCAT(CONCAT('(',REPLACE(LTRIM(RTRIM(REPLACE(COLUMNA, ',', ' '))), ' ', ',')),')') FROM Table1)

When I run this query I get invalid number error. Any suggestions ?

3
  • What is the data type of "ID" and "COLUMNA"? Commented Feb 20, 2020 at 16:38
  • @Acroyear The datatype of ID is BIGINT and the columnA is multilist column so it's VARCHAR daatype Commented Feb 20, 2020 at 16:40
  • There is your problem. You have to convert one side or the other so you are comparing like data types. Commented Feb 20, 2020 at 21:03

2 Answers 2

1

You get a number error because id is a number. So, Oracle wisely assumes that the subquery returns numbers.

I think the logic you want is:

SELECT ug.*
FROM USER_GROUP ug
WHERE EXISTS (SELECT 1
              FROM Table1 t1
              WHERE t1.COLUMNA LIKE '%,' || ug.ID || ',%'
             );

I strongly discourage you from storing number lists as strings. Just store one row per number. That is really much simpler and the resulting code will be more efficient.

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

5 Comments

I'm sorry to ask again but as I have marked it complete would you be able to also tell me how to use 2 exists in same query ? I want to use another column from Table1 with User Group table having same type of vaules
@MuhammadAsim . . . You would just add and exists . . . to the end of the query.
Well I was hoping that's would work but I don't understand query kept executing and doesn't stop. Well I'll have a look. Do I need to use User Group table again with another alias ? I tried both ways though
Does a NULL column affect this query if ColumnA is null
@MuhammadAsim . . . NULL will not match anything. That is expected behavior.
0

You can try converting "ID" using TO_CHAR, but you will lose the use of any index on ID if you have one.

SELECT * 
FROM USER_GROUP 
WHERE TO_CHAR(ID) IN (SELECT CONCAT(CONCAT('(',REPLACE(LTRIM(RTRIM(REPLACE(COLUMNA, ',', ' '))), ' ', ',')),')') FROM Table1)

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.