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';
Benki CanosoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jsaunCommented:
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.
slightwv (䄆 Netminder) Commented:
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

ThommyCommented:
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ThommyCommented:
Which solution did you choose?
slightwv (䄆 Netminder) Commented:
Benki Canoso,

Please provide more information about what you tried and what didn't work.

Here is a complete example of what I posted.  Please clarify the requirement.

SQL> create table tab1(Name_First varchar2(10), Name_Last varchar2(10),Team_no n
umber);

Table created.

SQL>
SQL>
SQL> insert into tab1 values('Joe','Doe',1);

1 row created.

SQL> insert into tab1 values('Scott','Miller',3);

1 row created.

SQL> insert into tab1 values('Mike','Smith',3);

1 row created.

SQL> insert into tab1 values('Jake','Willow',2);

1 row created.

SQL> insert into tab1 values('Joseph','Smaills',2);

1 row created.

SQL> insert into tab1 values('Tom','Cruise',4);

1 row created.

SQL> insert into tab1 values('Jeremy','Smith',4);

1 row created.

SQL> insert into tab1 values('Lori','Thompson',5);

1 row created.

SQL> commit;

Commit complete.

SQL>
SQL> undefine team_no
SQL> select * from tab1 where team_no in (
  2  select regexp_substr('&&team_no','[0-9]+',1,level)
  3  from dual
  4  connect by level <= length('&&team_no')-length(replace('&&team_no',','))+1
  5  )
  6  /
Enter value for team_no: 1,2,3
old   2: select regexp_substr('&&team_no','[0-9]+',1,level)
new   2: select regexp_substr('1,2,3','[0-9]+',1,level)
old   4: connect by level <= length('&&team_no')-length(replace('&&team_no',',')
)+1
new   4: connect by level <= length('1,2,3')-length(replace('1,2,3',','))+1

NAME_FIRST NAME_LAST     TEAM_NO
---------- ---------- ----------
Joe        Doe                 1
Scott      Miller              3
Mike       Smith               3
Jake       Willow              2
Joseph     Smaills             2

SQL>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Benki CanosoAuthor Commented:
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.
slightwv (䄆 Netminder) Commented:
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.
Benki CanosoAuthor Commented:
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
slightwv (䄆 Netminder) Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.