Conditional value replacement in SQL Server

Tavousi picture Tavousi · Dec 11, 2011 · Viewed 21.1k times · Source

I have a table that has several columns. The value of one column is 0 or 1. I want to write a query that returns "Hello" if the value was 0, or "Bye" if it was 1. What is the appropriate way to write this query?

Answer

Martin Smith picture Martin Smith · Dec 11, 2011

Use a CASE expression

SELECT CASE YourCol
         WHEN 0 THEN 'Hello'
         WHEN 1 THEN 'Bye'
       END AS SomeAlias
FROM   YourTable