【问题标题】:CASE inside where clause with null checking带有空检查的 where 子句中的 CASE
【发布时间】:2018-07-19 11:01:07
【问题描述】:

我想从具有空和非空条件的表中返回行。当我将列 closdt 等同于静态值时,它可以工作,但不适用于 case 语句内的空值检查条件。 closdt 是表 sbm 中的列

select * from sbm where
closdt  
case when :chk='Y' then 
  is null 
else 
  is not null 
end;

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    您可以使用一些布尔逻辑来编写您的条件:

    :chk='Y' and closdt is null 
    OR
    :chk is null
    OR
    :chk != 'Y'
    

    【讨论】:

      【解决方案2】:

      没有办法使用 CASE 表达式分解条件测试,如您所示。您可以使用 CASE 表达式生成要测试的值,如下所示:

      select *
        from sbm
        where case
                when :chk = 'Y' AND closdt IS NULL THEN 1
                when :chk <> 'Y' AND closdt IS NOT NULL THEN 1
                else 0
              end = 1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-06
        • 1970-01-01
        • 1970-01-01
        • 2018-11-29
        • 1970-01-01
        • 2017-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多