Solved

SQL Query Multilpe Values

Posted on 2014-12-02
11
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 77

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Thommy
ID: 40478208
Which solution did you choose?
0
 
LVL 77

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 77

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 77

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Workhours Count beetween Workhours 3 50
update using pipeline function 3 46
date show only hh:mm 2 50
Select the 2 most recent visit dates 5 31
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to recover a database from a user managed backup

734 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