Pergunta

I have a query where data is coming from front end into IN condition. Now the value is coming as comma separated for eg: 002,003 or 002q, 4335f, 123d, shd4 or yuw98

My query takes value as select * from tbl1 where userid in ('002,004') where as it should be userid in ('002','004')

I tried below query to replace the string but it doesnot work.

Select * from tbl1 where UserId in (''''|| Replace('004,002', ',', ''',''') || '''');

Same value if i use in

Select (''''|| Replace('004,002', ',', ''',''') || '''') from dual;

Returns
'004','002'

then why does the value not run in my original query ??

Foi útil?

Solução 3

For those who were looking for solution, I found one.

WHERE INSTRC('001, 002'), userid, 1, 1) > 0

Outras dicas

If you have a query like this:

 select * from tbl1 where userid in ('002,004')

And, for some reason, you have to represent the list of values as a string (assuming you want to search for two values in this case), then you can use like to find matches:

where ','||userid||',' like ','||'002,004'||','

I encourage you to try to find a way to make in work, by properly constructing the query. For one thing, this would allow the query to take advantage of an index. But, if not reasonable, then the like approach will work.

I tried a similar thing before, but soon learned that it is not possible in Oracle. So, i worked out another solution. Hope it doesn't sound too silly. Ok here it goes

  • Create separate variables based on your best assumption about no.of values the user inputs.
  • Write a simple block of pl/sql code to seperate the each value and store them into variables.
  • Use all these variables in your IN condition.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top