Вопрос

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