In an oracle database-table I need to find a result for a given lot-number.
The field where lot-numbers are saved is a string containing something like ‘1-3,5,10-15,20’ (the numbers inside this string are sorted)
is there a way of doing this?
in the example above, the result should be found for the following lot-numbers:
1,2,3,5,10,11,12,13,14,15,20
There is no way to do it in the application, so it has to be done inside the databse.
something like: “SELECT * FROM products WHERE lot = 2”
It is possible to do this all in SQL by use of the REGEXP_SUBSTR function and hierarchical queries:
However, I must emphasise that normalising your database properly is the way to go. This solution may not scale well and does a hugely unnecessary amount of work.
It works like this:
First split your data on the comma:
Next, split it on the hyphen to provide a minimum and maximum lot to use in the BETWEEN before finally joining it to the table. The NVL is there to ensure that there is always a maximum.
Here’s a working SQL Fiddle with the full query.