Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

NVL in where condition

ponicpoolJan 23 2018 — edited Jan 23 2018

Hello,

I am trying to use NVL in where condition and purpose are if the user is not entering a parameter in the where condition, then with the NVL function, all rows should be returned.

If the column is null, then NVL doesn't return all the rows, only the rows which are having values would be returned. So how can I use NVL or other built-in function which would return all the rows if the user has not entered a parameter value?

Let me explain with an example.

Case 1

SELECT *

FROM products

WHERE prod_id = NVL ( :param, prod_id);

The above query would return all the rows because prod_id does exist in all the rows, see the below-enclosed screenshot.

pastedImage_14.png

Case 2

SELECT *

FROM products

WHERE prod_desc = NVL ( :param, prod_desc)

This query would return only 4 rows as PROD_DESC for the fifth row does not exist

pastedImage_30.png

How can I get all the rows if the parameter value is empty or not being entered?

Table structure and data.

CREATE TABLE products

(

prod_id NUMBER,

prod_desc VARCHAR2 (32)

);

SET DEFINE OFF;

Insert into PRODUCTS

(PROD_ID, PROD_DESC)

Values

(1002, 'Pens');

Insert into PRODUCTS

(PROD_ID, PROD_DESC)

Values

(2002, 'Inks');

Insert into PRODUCTS

(PROD_ID, PROD_DESC)

Values

(3002, 'Books');

Insert into PRODUCTS

(PROD_ID, PROD_DESC)

Values

(4002, 'Pencils');

Insert into PRODUCTS

(PROD_ID)

Values

(5002);

COMMIT;

This post has been answered by Frank Kulash on Jan 23 2018
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 20 2018
Added on Jan 23 2018
10 comments
27,407 views