?
Solved

oracle where clause

Posted on 2010-11-23
3
Medium Priority
?
507 Views
Last Modified: 2012-05-10
experts---
I have to code a where clause in oracle which has the following cases

1. If the input variable is null ignore this column in the where clause
2. if the input variable has value then include this column in the where clause.

ex: select a,b,c
      from table_a
where col_a = input1
and col_b = (if input2 has a value then do the and col_b = input2
else dont )

0
Comment
Question by:chickanna
[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
3 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34199414
select a,b,c
from table_a
where col_a = input1
and (input2 is null OR col_b = input2)
0
 
LVL 35

Expert Comment

by:johnsone
ID: 34199593

Alternately....
select a,b,c
  from table_a
 where col_a = input1
   and col_b = nvl(input2, col_b)

Open in new window

0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 500 total points
ID: 34200605
You can also use coalesce
select a,b,c
  from table_a
 where col_a = input1
   and col_b = coalesce(input2, col_b)
 
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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

770 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