Nick Loach
Nick Loach

Reputation: 333

Return all rows if null value is passed

I have a query like this:

select empno,name
from emp
where job = 'CLERK'
and empno = :empno

If I pass empno that is null I would like to display all the records that match the condition of job = 'CLERK'. If empno is a specific number then it should filter for job and empno.

Anyway to do this in SQL without using PLSQL?

Upvotes: 3

Views: 6251

Answers (2)

Pranay Rana
Pranay Rana

Reputation: 176896

Something like this if pass parameter is null than replace it with the actual column value ...

select empno,name from emp where 
job = 'CLERK' 
and empno = NVL(:empno ,empno)

How NVL work

The syntax for the NVL function is:

NVL( string1, replace_with )

string1 is the string to test for a null value.

replace_with is the value returned if string1 is null.

Upvotes: 3

Andomar
Andomar

Reputation: 238086

and (empno = :empno or :empno is null)

Upvotes: 7

Related Questions