Question

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?

Was it helpful?

Solution

Use a CASE expression

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

OTHER TIPS

If you choose multi/all columns, please try with below:

SELECT Column1, Column2,  -- Put other column name here 
        CASE TargetColumnName
         WHEN 0 THEN 'Hello'
         WHEN 1 THEN 'Bye'
       END AS TargetAliasColumnName
FROM   YourTableName 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top