• Status: Solved
• Priority: Medium
• Security: Public
• Views: 468

# conditional where clause

Hi,

I want to write something like this

Select * from XYZ
Where
a = 10
b=20
if(c!= null)
and c = 30

(if c is null then I do not want any condition on 'c' in my where clause)
0
hipal
7 Solutions

Commented:
If 'c = 30' in the where clause it cannot be null so no need for the check.

I figure you've tried to simplify the question but think you might have over simplified it.  Can you provide a little more detail?

Are you asking that if a 'paramter' for c is not provided, then do not check column c in the where?

Typically you need dynamic SQL for this but I think you can do it with someting like this, ssuming 30 is a paramter value and not hardcoded:

select * from XYZ
where a=10 and b=20 and c = case when 30 is null then 30 else c end;
0

Commented:
select * from xyz where a=10 and b = 20 and(c is null or c = 30);
0

Data EngineerCommented:
Check if you want AND or OR operation between the filters.
``````SELECT *
FROM XYZ
WHERE a = 10
AND b = 20
AND NVL(c,30) = 30
``````
0

Commented:
To help clarify, can you post some sample data and the expected output?
0

Commented:
Select * from XYZ
Where a = 10
and b = 20
and nvl(c, 30) = 30
0

Commented:
Sorry, I withdraw my submission; realized that the exact same solution is provided by Sharath_123
0

Commented:
subratabiswas,

How is that different from what Sharath_123 posted?

Please review previous posts before posting.
0

Commented:
You caught me typing...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.