Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> Re: Something i'm missing

Re: Something i'm missing

From: Rumpi Gravenstein <rgravens_at_gmail.com>
Date: Wed, 15 Aug 2007 13:10:20 -0400
Message-ID: <9c9b9dc90708151010x4412dd40mc84388e0a35029cc@mail.gmail.com>


<snip>
select count(*) from all_users where username = nvl( '&enter_user_name', username);
</snip>

Ahhh, what about that three value NULL bug-a-boo...

SQL> CREATE TABLE test( col varchar2(1) )   2 /

Table created.

SQL> INSERT INTO test VALUES ( null )
  2 /

1 row created.

SQL> INSERT INTO test VALUES ( 'Y' )
  2 /

1 row created.

SQL> COMMIT
  2 /

Commit complete.

SQL> SELECT COUNT(*)
  2 FROM test
  3 /

  COUNT(*)


         2

SQL> SELECT COUNT(*)
  2 FROM test
  3 WHERE col = NVL(NULL, col)
  4 /

  COUNT(*)


         1

SQL> It strikes me that the more robust answer must make allowances for the NULL match like:

SELECT count(*)
  FROM test
WHERE ( '&A' IS NULL OR col = '&A')

which results in:

SQL> SELECT COUNT(*)
  2 FROM test
  3 WHERE ( '' IS NULL OR col = '' );

  COUNT(*)


         2

SQL> SELECT COUNT(*)
  2 FROM test
  3 WHERE ('Y' IS NULL OR col = 'Y' );

  COUNT(*)


         1

Rumpi Gravenstein

--
http://www.freelists.org/webpage/oracle-l
Received on Wed Aug 15 2007 - 12:10:20 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US