Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top