SQL 在预期条件的上下文中指定的非布尔类型的表达式,靠近“then”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15078704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:54:54  来源:igfitidea点击:

An expression of non-boolean type specified in a context where a condition is expected, near 'then'

sqlsql-serverselectsql-server-2008-r2

提问by Andrey

I was checking alot of exist questions wiht similar error but cannot find anwer which can help me with my error I have select with cases

我正在检查很多存在类似错误的问题,但找不到可以帮助我解决错误的答案 我选择了案例

select 
    case 
    when e.EthnCode in ('N','A','B','P','W') then ethnicity
    else 'Multi' 
    end as Ethnicity,
    case when cat.CategCode IN ('CH','IN','NB','PG','PP','SR')then Category else 0 end as ' ',
 --COUNT (c.EthnCode) as '(A) tottal Numbers of participamts by Rase', 
 sum(case when C.StatusID =1 or C.StatusID =2 then 1 else 0 end),
 sum(case when c.Hispanic then 1 else 0 end) as 'Hisp'
from  Ethnicity E
LEFT JOIN Clients C
ON c.EthnCode = e.EthnCode 
LEFT join Category cat 
ON c.CategCode = cat.CategCode
where c.StatusID = 1 
group by case 
    when e.EthnCode in ('N','A','B','P','W') then ethnicity
    else 'Balance Reporting More Than One Race' 
    end

and it throws error

它抛出错误

Msg 4145, Level 15, State 1, Line 9
An expression of non-boolean type specified in a context where a condition is expected, near 'then'.

Line 9

9号线

 sum(case when c.Hispanic then 1 else 0 end) as 'Hisp'

it is underlining Hispanic Please need help :)

它强调西班牙裔请需要帮助:)

采纳答案by Kaf

 sum(case when c.Hispanic then 1 else 0 end) as 'Hisp'

Change as (please use the correct value and data type in the comparison)

更改为(请在比较中使用正确的值和数据类型)

 sum(case when c.Hispanic = 1 then 1 else 0 end) as 'Hisp'

Or to a simple case as

或者作为一个简单的案例

 sum(case c.Hispanic when 1 then 1 else 0 end) as 'Hisp'

回答by Tony Hopkinson

It doesn't think C.Hispanic is a boolean. Case When C.Hispanic = 1 Then....

它不认为 C.Hispanic 是一个布尔值。 Case When C.Hispanic = 1 Then....

or some such will sort it out.

或者一些这样的人会解决它。