oracle中如何让null等于null

发布于 2024-11-28 22:13:33 字数 650 浏览 1 评论 0原文

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

例如,

表 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 表现出这种方式?

I have a variable being passed to my stored proc and it's a filter (basically). However, that field can sometimes be null, and if it is, I want to be able to check against the rows that have that field as null.

For example,

Table A:

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

The query (With inputs, val, filter):

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

Expected I/O:

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

How can I make Oracle behave this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花ソ最美的依靠 2024-12-05 22:13:33

怎么样:

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,基本上会使查询匹配。

How about:

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))

I'm not an Oracle expert, but I'd expect that to work - basically make the query match if both the filter and the value are NULL.

苏璃陌 2024-12-05 22:13:33

Oracle 没有 ISNULL 函数。所以你需要类似的东西

SELECT COUNT(*)
  FROM tableA
 WHERE brn_brand_id = filter
    OR (    brn_brand_id IS NULL 
        AND filter IS NULL)

Oracle doesn't have an ISNULL function. So you'd need something like

SELECT COUNT(*)
  FROM tableA
 WHERE brn_brand_id = filter
    OR (    brn_brand_id IS NULL 
        AND filter IS NULL)
满栀 2024-12-05 22:13:33

有时这也很方便:

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

This can also be handy at times:

   SELECT COUNT(*)
      FROM tableA
   WHERE
      NVL(brn_brand_id, CHR(0)) = NVL(filter, CHR(0))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文