简体   繁体   中英

Oracle Query - Replace not working

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 ??

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.

对于那些正在寻找解决方案的人,我找到了一个。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM