带大小写的SQL Where语句不打印任何记录

带大小写的SQL Where语句不打印任何记录,sql,Sql,我正在尝试创建一个包含Where语句的SQL查询。在Where语句中,我想检查一个子句。查询结果如下: select * from new_payment where new_totalprofit>@totalprofit and (RTRIM(LTRIM(left(P.new_name ,(CHARINDEX('-',P.new_name)-1)))) = case when @paymenttype<>'All'

我正在尝试创建一个包含Where语句的SQL查询。在Where语句中,我想检查一个子句。查询结果如下:

select * 
from new_payment
where  new_totalprofit>@totalprofit 
  and (RTRIM(LTRIM(left(P.new_name ,(CHARINDEX('-',P.new_name)-1)))) =
         case 
            when @paymenttype<>'All' 
            then @paymenttype end)
我检查了该查询中是否存在记录,但没有显示任何记录。我遗漏了什么?

如果@paymentType等于All,您的CASE表达式将返回NULL,因此您将不会得到任何结果。您不需要大小写表达式:

select * 
from new_payment
where  new_totalprofit>@totalprofit 
  and (RTRIM(LTRIM(left(P.new_name ,(CHARINDEX('-',P.new_name)-1)))) =
         case 
            when @paymenttype<>'All' 
            then @paymenttype end)
select * from new_payment
 where  new_totalprofit>@totalprofit and
 (
    @paymenttype='All' or
    (RTRIM(LTRIM(left(P.new_name ,(CHARINDEX('-',P.new_name)-1)))) =
    @paymenttype)
 )

表中的数据是什么?提供给@paymenttype的值是多少?在new_paymenttype列中有两种类型的记录。当@payment='all'我想做的事情没有检查任何东西时,它们是“信用卡”和“里程点”。您认为我上面显示的查询不符合该要求吗?如果没有,为什么?哦,对不起。现在我得到了您的代码。您正在避免返回null。@paymenttype='All'子句返回true或false,这对我很有帮助