?
Solved

Using DECODE in a where clause

Posted on 2003-10-29
6
Medium Priority
?
1,967 Views
Last Modified: 2009-05-15
Using DECODE in where clause

I have a where clause that I want to be based on a parameter value. I could have different queries but I thought that I would try DECODE. The other challange is that the decode is in an IN clause. Here is the code.

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and CT.trip_grade in
(decode ('&GradeName','PreK','1',
'K','(1,2)',
'1','(''1'',''2'')',
'2','1,2',
'3','1,2,3',
'4','2,3',
'5','2,3',
'6','2,3,4',
'7','3,4',
'8','3,4',
'9','3,4',
'10','4',
'11','4',
'12','4','12345'));

This works fine if I enter PreK, 10, 11 or 12, but any other doesn't work. How can I introduce the list of options in the IN clause usint the DECODE THEN portion if I have more than one to return?

I tried two other approaches with 'K', and '1', but they don't work.

Angel

0
Comment
Question by:aleyva
[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
6 Comments
 
LVL 15

Expert Comment

by:andrewst
ID: 9643772
The problem is that "colname IN ('1,2,3')" doesn't have the same menaing as "colname IN (1,2,3)".  The first list has ONE varchar2 element '1,2,3' and the second has THREE elements 1, 2 and 3.

You could rewrite like this:

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and (&GradeName = 1 and CT.trip_grade in (1,2)
      or &GradeName = 2 and CT.trip_grade in (1,2)
      or ... etc. etc.
      )

0
 
LVL 5

Expert Comment

by:FBIAGENT
ID: 9643901
or

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and substr(decode ('&GradeName','PreK','1',
'K','1,2',
'1','1,2',
'2','1,2',
'3','1,2,3',
'4','2,3',
'5','2,3',
'6','2,3,4',
'7','3,4',
'8','3,4',
'9','3,4',
'10','4',
'11','4',
'12','4','12345'),CT.trip_grade,1)>=1;
0
 

Author Comment

by:aleyva
ID: 9643907
What about something like "colname in ('1','2','3')"

How can I get DECODE to return that type of result?

Would the DECODE be faster or the OR?

A
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 15

Expert Comment

by:andrewst
ID: 9643993
If trip_grade is a varchar2 then:

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and (&GradeName = 1 and CT.trip_grade in ('1','2')
      or &GradeName = 2 and CT.trip_grade in ('1','2')
      or ... etc. etc.
      )

In your original post, you sometimes had (1,2) and sometimes had ('1','2'), which I found odd.  Column trip_grade is either a NUMBER or a VARCHAR2, it can't be both.

As for whether the DECODE or OR is faster (assuming you could get the DECODE to work), I don't suppose it makes any difference: both will cause a full table scan.


0
 
LVL 8

Accepted Solution

by:
Danielzt earned 2000 total points
ID: 9643994
Can you try to create a table to save these values? this will make your coding and maintaining easy.

let's you use table grade_mapping
create table grade_mapping
( grade_name varchar2(20), grade number(2))

insert into table grade_mapping
valuse('Prek',1);
insert into table grade_mapping
valuse('K',1);
insert into table grade_mapping
valuse('K',2);
insert into table grade_mapping
valuse('1',1);
insert into table grade_mapping
valuse('1',2);
.....

So your query will be:

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and CT.trip_grade in
(select grade from grade_mapping where grade_name= '&GradeName')

0
 

Author Comment

by:aleyva
ID: 9644757
Well, the reason for the odd coding in my example is because I tried those three different approaches, sorry I wasn't clear on that.

What I am going to consider is the table mapping solution. This will make the maintance much easier because if the mapping changes, then all I have to do is change the data in the database, and not  have to change the Forms code.

Let's see how this works out for me.

A
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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…
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…
Suggested Courses

762 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