Selecting with CASE
suggest changeWhen 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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents