【发布时间】:2019-03-19 14:45:13
【问题描述】:
我正在尝试做这样的事情:
select
col_1, col_2, etc
from
table
where
col_1 = nullif('', '')
我做错了吗?我没有得到任何结果。
编辑:
我的预期结果是返回 col_1 为 NULL 的每条记录。
我知道我可以使用 col_1 为空的地方,但我使用的是 SSIS 和一个变量。有时 col_1 实际上是 NULL,有时不是。
样本数据:
collaboration first_name last_name city
NULL Bob Smith Chicago
Data Migration John Smith Austin
NULL Pika Chu Houston
Production ash ketchum tokyo
有时我可能想返回协作为 NULL 的记录,有时我想返回显示为 Production 的记录。
如果可能的话,我想使用相同的查询,只需稍作修改。
编辑第 2 部分:
我尝试过这个。
select
col_1, col_2, etc
from
table
where
case
when col_1 = '' then NULL
else col_1
end
但我收到错误消息:
An expression of non-boolean type specified in a context where a condition is expected, near ORDER.
查询速度不是我关心的。
【问题讨论】:
-
您的数据和预期结果是什么?
-
你想做什么?你看过documentation关于如何使用NULLIF()吗?
-
col_1 = nullif('', '')与Col_1 = NULL相同,永远不会返回true;因为没有任何东西等于NULL(包括NULL)。 -
你说知道你可以使用
where col_1 is null,但我不明白你为什么不能。请展示 SSIS 中存在变量阻止您这样做的实际情况。 -
如果你能给我们一些样本数据和预期的结果,那就容易多了。
标签: sql sql-server nullif