简体   繁体   English

SQL WHERE CASE 子句中的表达式

[英]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.我想从具有两个字段namenullable_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM