Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Oracle SQL Case with Null

I have a simple query that is taking an input from a bind variable.

CREATE TABLE "FRUITS" 
(   "FRUIT_NAME" VARCHAR2(100), 
"COLOR" VARCHAR2(100)
) ;

insert into fruits (fruit_name, color)
values ('Banana', 'Yellow')
insert into fruits (fruit_name, color)
values ('Lemon', '')
insert into fruits (fruit_name, color)
values ('Apple', 'Red')

SELECT * FROM FRUITS
WHERE
COLOR =     case
            when :P1_ITEM is null then null
            else :P1_ITEM
            end

If the input is ‘Yellow’ the result would be ‘Banana’ (when ‘Red’ then ‘Apple’). However, if the input happens to be null the result is ‘no data found’.
How can this be avoided knowing that null is not a null value?
If the input is null on color then how can I return the null color row? meaning ‘Lemon’ + null

Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Something like this might be one option:

SELECT * FROM FRUITS
WHERE
nvl(COLOR, 'x') = case
                    when :P1_ITEM is null then 'x'
                    else :P1_ITEM
                  end;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading