2014-08-29 34 views
-1

我有产品的列表,并想创建一个语法为:如何处理字符串不等于...然后空白?

case when product is not 'apple' or product is not 'pear' or product is not 'plum' then 0 else product end 

我如何做到这一点? 我试过

case when product <> 'apple' 
    when product <> 'pear' 
    when product <> 'plum' then 0 
    else product 
end 

但这似乎不起作用。

回答

3

像这样的东西?

SELECT CASE 
      WHEN Product NOT IN ('Apple', 'Pear', 'Plum') THEN 0 
      ELSE Product 
     END 
FROM ... 
0

就像你问的那样,用“OR”分隔条件而不是“WHEN”。

+0

明白了!谢谢大家!但我还有一个问题。所以我们有各种各样的产品,包括水果和纸张等未过期的东西。 产品---------有效期 我该如何做案例陈述,以显示:产品不是苹果或产品不是橙色,然后填写到期1的情况? – RoseChris 2014-08-29 17:56:17

+0

@ user3508386您应该更新您的问题或创建另一个问题,因为您现在有新的需求。另外,选择更适合您需要的答案并将其标记为一个答案。 – 2014-08-29 18:02:30

1

我不知道你的表中的字段,但它会是这个样子

SELECT case when (Product <> 'apple' 
        OR Product <> 'pear' 
        OR Product <> 'plum' 
       ) then 0 else Product end as ProductName 
FROM tableName