Solved

SQL Query Multilpe Values

Posted on 2014-12-02
11
192 Views
Last Modified: 2014-12-06
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';
0
Comment
Question by:Benki Canoso
  • 4
  • 2
  • 2
  • +1
11 Comments
 
LVL 1

Expert Comment

by:jsaun
ID: 40477573
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.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40477601
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

0
 
LVL 19

Expert Comment

by:Thommy
ID: 40478117
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
0
 
LVL 19

Expert Comment

by:Thommy
ID: 40478208
Which solution did you choose?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40479753
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

0
 

Author Comment

by:Benki Canoso
ID: 40479778
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.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40479803
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.
0
 

Author Comment

by:Benki Canoso
ID: 40479876
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
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40479973
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now