CASE的WHERE子句

时间:2017-11-03 01:32:43

标签: sql-server case

我正在使用SQL Server 2016,我有一个如下所示的表:

Create table testA
(
    ID int not null,
    OriginalValue int null
)

Insert into testA (ID, OriginalValue) 
values (1, 10), (2, 10), (3, 10), 
       (4, 10), (5, 10), (6, null), (7, null)

我需要根据

显示价值
Declare @intExclude int = 1

Select  ID,
        OriginalValue as OriginalValue
From    testA
Where
--@intExclude = 1
Where  IsNull(OriginalValue,0) > 0 

--@intExclude = 0
where   isnull(Originalvalue,0) = 0 or IsNull(originalvalue,0) > 0 

当排除为0时,如何显示所有值,而当排除为1时,如何显示o记录 我已经尝试了where子句的案例,但没有成功?

由于 Oded Dror

4 个答案:

答案 0 :(得分:0)

这是你想要的吗?

Where (@intExclude = 1 and coalesce(OriginalValue, 0) > 0) ) or
      (@intExclude = 0 and coalesce(Originalvalue, 0) >= 0 )

答案 1 :(得分:0)

我想你需要这个:

Declare @intExclude int = 1

Select  ID, OriginalValue
From    testA
Where (@intExclude = 1 and OriginalValue is not null)
    or (@intExclude = 0)

答案 2 :(得分:0)

请试试这个

Where
(@intExclude = 1
and  IsNull(OriginalValue,0) > 0 ) or

(@intExclude = 0
and   isnull(Originalvalue,0) >= 0) 

答案 3 :(得分:0)

如果适用于你,试试这个吗?

Where (@intExclude = 1
    and IsNull(OriginalValue,0) > 0 ) OR (@intExclude = 0
    and (isnull(Originalvalue,0) = 0 or IsNull(originalvalue,0) > 0 ));
相关问题