Link to home
Start Free TrialLog in
Avatar of Benki Canoso
Benki Canoso

asked on

SQL Query Multilpe Values

I have a table that contains information about teams. I want to be able to write a SQL query (Oracle SQL Developer) that allows for multiple search entries for the same column using values separated by a comma.
Here is what I have:
Name_First     Name_Last   Team_no
Joe                    Doe                  1
Scott                Miller                3
Mike                Smith                3
Jake                  Willow              2
Joseph             Smaills             2
Tom                 Cruise               4
Jeremy             Smith               4
Lori                  Thompson       5

I want to have a query that reads similar to the below, but that it also allows for multiple comma-separated entries on the Team_no field:
SELECT * FROM Teams
WHERE Team_no = '&Team_no';
Avatar of jsaun
jsaun
Flag of United States of America image

You'll want to use the IN operator:

SELECT
    Name_First  ,  
    Name_Last   ,
    Team_no
FROM
    Teams
WHERE
Team_no in ('Value1','Value2','Value3')

Always enumerate your select columns, never use *
Your app server code will need to format your list before building the SQL statement, and be sure to check for null values.
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Using IN won't work that way for a comma separated variable.

You need to convert the comma separated variable into a 'table of values'.

Try this:
undefine team_no
select * from tab1 where team_no in (
select regexp_substr('&&team_no','[0-9]+',1,level)
from dual
connect by level <= length('&&team_no')-length(replace('&&team_no',','))+1
)
/

Open in new window

Add commas to  start and end of your variable, which contains the comma-separated values.
Do the same with your search field and use Oracle function INSTR() to check for each record, if the search field value is in the comma-separated list of values...

SELECT * FROM Teams where INSTR( ','||&Team_no||',' , ','||to_char(Team_no)||','  )>0
Which solution did you choose?
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Benki Canoso

ASKER

This is the query that I tried to run:

SELECT  * FROM Teams
WHERE team_no
IN ( Select regexp_substr('&team_no' , '[0-9]+1',1,level)
FROM dual
connect by level <= length('&team_no' ) - length(replace('&team_no'  , ',') ) + 1

)
/

For some reason I kept getting an error. I just simply want to allow a user to enter (if he chooses to) comma separated numeric values and then pull the records that contain each one of those numbers in them.
What is the error?
What tool/app/product are they entering the value?
How is that tool/app/product interfacing with Oracle?
What version or Oracle are you using(client side and server side)?

What I posted was for sqlplus variables and tools that know how to deal with them.
Let me give it another try to see if it works for me now. I use oracle 11g.
Can you please explain to me what is the following piece of code is actually doing?
select regexp_substr('&&team_no','[0-9]+',1,level)
from dual
connect by level <= length('&&team_no')-length(replace('&&team_no',','))+1
It takes your list of values and converts them into a 'table' that you can select against.

First look at:  length('&&team_no')-length(replace('&&team_no',','))+1

This tells you how many values you have in the list.
 length('&&team_no') is how many characters are in the list.
replace('&&team_no',',') replaces all the ',' with null values (strips them out).

Subtract the values and the result is the number of values in the list.

"connect by level" is a simple looping mechanism that tells Oracle to repeat operations.  This is what gives you the 'table' to return the correct number of rows.

A simple example is(return 10 rows):
select level from dual connect by level <= 10;

regexp_substr('&&team_no','[0-9]+',1,level)

"[0-9]+" one or more numbers

the substr says start at position '1' and look for 'level' occurrence.

Putting it all together:
First time through the loop (connect by level), starting at position 1, given the first occurrence of numbers.

Second time through, the second occurrence, etc...

The online docs can probably explain each function better than I can.