Solved

oracle where clause

Posted on 2010-11-23
3
506 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 125 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 125 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
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.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

726 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