简体   繁体   中英

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.

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). So I want one of two WHERE expressions to take place, depending on the situation.

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() :

where coalesce(item.nullable_name, item.name) like '%filter%'

coalesce() is a function that returns the first non- NULL value.

Try Where isnull(item.nullable_name, item.name) like '%filter%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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