문제

I need to pass a varchar through a case statement in SQL. The Current data is an integer but I would like to display text.

CASE b.religious_belief
WHEN 01 THEN 'none'     
WHEN 05 THEN 'roman catholic'    
 ELSE 0 
END

I keep getting the error

Conversion failed when converting the varchar value 'none' to data type int.

도움이 되었습니까?

해결책

All returned values from the CASE clauses should be strings (varchar)

CASE b.religious_belief
    WHEN 01 THEN 'none'     
    WHEN 05 THEN 'roman catholic'    
    ELSE 'Jedi' 
END as MyColumnName

다른 팁

CASE b.religious_belief
    WHEN 01 THEN 'none'     
    WHEN 05 THEN 'roman catholic'    
    ELSE '' 
END

The return for the else also needs to be a varchar, so if you're after a "zero" value, then either '' or null would be best.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top