Selecting with CASE

suggest change

When results need to have some logic applied ‘on the fly’ one can use CASE statement to implement it.

SELECT CASE WHEN Col1 < 50 THEN 'under' ELSE 'over' END threshold
FROM TableName

It can also be chained. In the next expression, 50 and 100 match neither WHEN, so both fall through to over.

SELECT 
    CASE WHEN Col1 < 50 THEN 'under' 
         WHEN Col1 > 50 AND Col1 <100 THEN 'between' 
         ELSE 'over' 
    END threshold
FROM TableName

one also can have CASE inside another CASE statement

SELECT 
    CASE WHEN Col1 < 50 THEN 'under' 
         ELSE 
            CASE WHEN Col1 > 50 AND Col1 <100 THEN Col1 
            ELSE 'over' END 
    END threshold
FROM TableName

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents