954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Comma-separated filter

Hi,

I have a select statement and I need to find a way to apply a filter to it:

select * from my_table where my_table.field_1 like :filter_item


The real problem is that filter_item can be a string, a string that includes '%' or a comma-separated string that can include wildcard like this:

abc, abgf%fri, e%ij%fd, abcd

The best way would be to solve this without creating any special package/function, etc. But if there is no other way then any solution is welcome.

Thanks,
Ender

end3r
Light Poster
37 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

so, what is the problem ?

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

The problem is that the :filter_item = 'abc, abgf%fri, e%ij%fd, abcd'.
So the select will look like:

SELECT * FROM my_table WHERE my_table.field_1 LIKE 'abc, abgf%fri, e%ij%fd, abcd'

I need to split this and have the following functionality:

SELECT * FROM my_table 
WHERE my_table.field_1 LIKE 'abc'
OR my_table.field_1 LIKE 'abgf%fri'
OR my_table.field_1 LIKE 'e%ij%fd'
OR my_table.field_1 LIKE 'abcd'


Do you have a solution for this ?

end3r
Light Poster
37 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

you have to create a procedure / function for that.

debasisdas
Posting Genius
6,872 posts since Feb 2007
Reputation Points: 666
Solved Threads: 434
 

I think there is a solution using the REGEXP_LIKE function. The only problem is that I am not sure what regular expression should be used.

end3r
Light Poster
37 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

Generally filterItem is being passed in from code (PHP, JSP, C#, etc.).

This would be the place to break that out into an array, separating on commas, then either reparse into separate "OR"s or pass in as separate parameters.

I'm guessing you're doing some sort of search on keywords?

Brillig
Light Poster
48 posts since Feb 2010
Reputation Points: 10
Solved Threads: 6
 

Hy,

I finally found the solution, so in case anyone encounters the same problem, here is the condition for searching an IP_ADDRESS column by a comma-separated list of IPs:

-- YOUR CODE....
AND REGEXP_LIKE(IP_ADDRESS, REPLACE(REPLACE(REPLACE(REPLACE(NVL(:IP_ADDRESS_LIST, '%.%.%.%'), ', ', '$|'), ',', '$|'), '%', '[0-9]{1,3}'), '.', '\.') || '$' )


Best Regards,
Ender

end3r
Light Poster
37 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: