testing inequality with columns that can be null

后端 未结 8 1239
不思量自难忘°
不思量自难忘° 2021-02-05 14:48

So, I asked a question this morning, which I did not phrase correctly, so I got a lot of responses as to why NULL compared to anything will give NULL/FALSE.

My actual q

8条回答
  •  耶瑟儿~
    2021-02-05 15:29

    IBM Informix Dynamic Server has a somewhat peculiar view of booleans for a variety of historical (aka 'bad') reasons. Adapting the idea suggested by @astander, this CASE expression 'works', but I'd be the first to say 'not obvious' (see - I said it before you did!). The setup phase:

    create table x(a int, b int);
    insert into x values(null, null);
    insert into x values(null, 1);
    insert into x values(1, null);
    insert into x values(1, 1);
    insert into x values(1, 2);
    

    The SELECT statement:

    SELECT *
      FROM x
      WHERE   CASE
              WHEN a IS NULL AND b IS NULL THEN 'f'::BOOLEAN
              WHEN a IS NULL OR  b IS NULL THEN 't'::BOOLEAN
              WHEN a != b                  THEN 't'::BOOLEAN
              ELSE                              'f'::BOOLEAN
              END
    ;
    

    The result from this query is:

                     1
          1           
          1          2
    

    Issues:

    • IDS does not recognize FALSE or TRUE or UNKNOWN as keywords.
    • IDS does not recognize boolean expressions such as 'a != b' (or 'a <> b') as such.

    Yes, it pains me greatly to have to state this.

提交回复
热议问题