4
votes

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

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

it is underlining Hispanic Please need help :)

2
What type is c.Hispanic? The expression case when c.Hispanic should be case when c.Hispanic = [something]Mike Park

2 Answers

3
votes
 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'
1
votes

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

or some such will sort it out.