[英]SQL WHERE Expression in CASE clause
I want to fetch data from a table Item
that has the two fields name
and nullable_name
, the second one can be empty.我想从具有两个字段
name
和nullable_name
的表Item
中获取数据,第二个可以为空。
Now I want to add a filter string, so that only results will get fetched that contain that string (in the name
field if nullable_name
is null, and in the nullable_name
field if it is present).现在我想添加一个过滤器字符串,以便只获取包含该字符串的结果(如果
nullable_name
为 null,则在name
字段中,如果存在,则在nullable_name
字段中)。 So I want one of two WHERE expressions to take place, depending on the situation.所以我希望根据情况发生两个 WHERE 表达式之一。
What I tried but didn't work:我尝试过但没有用的方法:
SELECT * FROM Item
WHERE
CASE
WHEN Item.nullable_name IS NULL THEN (Item.name LIKE '%filter%')
ELSE (Item.nullable_name LIKE '%filter%')
END
You seem to want coalesce()
:你似乎想要
coalesce()
:
where coalesce(item.nullable_name, item.name) like '%filter%'
coalesce()
is a function that returns the first non- NULL
value. coalesce()
是一个 function,它返回第一个非NULL
值。
Try Where isnull(item.nullable_name, item.name) like '%filter%'试试 Where isnull(item.nullable_name, item.name) like '%filter%'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.