Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Query Multilpe Values

Posted on 2014-12-02
11
Medium Priority
?
202 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 78

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 19

Expert Comment

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

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 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 78

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 78

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

971 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