如何在oracle中使null等于null

时间:2011-08-08 17:56:54

标签: oracle plsql

我有一个变量传递给我的存储过程,它是一个过滤器(基本上)。但是,该字段有时可以为null,如果是,我希望能够检查具有该字段的行为空。

例如,

表A:

VALUE_COLUMN | FILTER_COLUMN
----------------------------
A            |  (NULL)
B            |  (NULL)           
C            |  (NULL)
D            |  (NULL)
A            |  1
E            |  (NULL)
F            |  (NULL)
B            |  1

查询(带输入,val,过滤器):

SELECT  COUNT(1)
  FROM    TableA
WHERE 
  wrap_up_cd = val
  AND brn_brand_id = filter

预期的I / O:

val = A, filter = (null) = 1
val = A, filter = 1 = 1
val = C, filter = 1 = 0

如何让Oracle以这种方式行事?

3 个答案:

答案 0 :(得分:8)

怎么样:

SELECT  COUNT(1)
  FROM    TableA
WHERE 
  wrap_up_cd = val
  AND ((brn_brand_id = filter) OR (brn_brand_id IS NULL AND filter IS NULL))

我不是Oracle专家,但我希望能够工作 - 如果过滤器的值都为NULL,则基本上使查询匹配。< / p>

答案 1 :(得分:2)

Oracle没有ISNULL功能。所以你需要像

这样的东西
SELECT COUNT(*)
  FROM tableA
 WHERE brn_brand_id = filter
    OR (    brn_brand_id IS NULL 
        AND filter IS NULL)

答案 2 :(得分:0)

这有时候也很方便:

   SELECT COUNT(*)
      FROM tableA
   WHERE
      NVL(brn_brand_id, CHR(0)) = NVL(filter, CHR(0))