【发布时间】: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;
【问题讨论】:
我想从具有空和非空条件的表中返回行。当我将列 closdt 等同于静态值时,它可以工作,但不适用于 case 语句内的空值检查条件。 closdt 是表 sbm 中的列
select * from sbm where
closdt
case when :chk='Y' then
is null
else
is not null
end;
【问题讨论】:
您可以使用一些布尔逻辑来编写您的条件:
:chk='Y' and closdt is null
OR
:chk is null
OR
:chk != 'Y'
【讨论】:
没有办法使用 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
【讨论】: