The Case Expression

Contact Us or call 1-877-932-8228
The Case Expression

The Case Expression

Code Sample:

Conditional-Processing/Demos/case_expression.sql
SELECT first_name, last_name, 
	CASE first_name 
		WHEN 'Adam' THEN 'is first'
		WHEN 'Alana' THEN 'is next'
		ELSE 'follows' 
	END AS statement
FROM employees
ORDER BY first_name, last_name;

The CASE statement can function in exactly the same way as DECODE.

Code Sample:

Conditional-Processing/Demos/case_expression_range.sql
SELECT 
	first_name, 
	last_name, 
	salary, 
	CASE  
  		WHEN salary > 20000 THEN 'very high'
  		WHEN salary > 10000 THEN 'high'
  		WHEN salary < 2600 THEN 'low'
  		ELSE  'average'
  	END AS category
FROM employees
ORDER BY first_name, last_name;

Besides doing exact matches, a CASE can be used to evaluate ranges of data.

Next