© Belgium Campus 2021
© Belgium Campus 2021
©©BELGIUM
BELGIUMCAMPUS
CAMPUS2021
2021
2
END
CASE <Case_Expression>
CASE is the extension of FALSE WHEN Value_1 THEN Statement_1
IF...ELSE statement. Unlike WHEN Value_2 THEN Statement_2
CASE_Expression = TRUE .
IF…ELSE, where only the Statement_2
Value_2 WHEN Value_N THEN Statement_N
maximum of one condition [ELSE Statement_ELSE]
is allowed, CASE allows the END
user to apply multiple FALSE
conditions to perform
CASE_Expression = TRUE CASE <Case_Expression>
different sets of actions. Statement_N WHEN Value_1 THEN Statement_1
Value_N
WHEN Value_2 THEN Statement_2
.
WHEN Value_N THEN Statement_N
FALSE [ELSE Statement_ELSE]
END
Statements_Else
CASE <Case_Expression>
WHEN Value_1 THEN Statement_1
WHEN Value_2 THEN Statement_2
CASE END .
WHEN Value_N THEN Statement_N
[ELSE Statement_ELSE]
END
© BELGIUM CAMPUS 2021
6
CASE expression
WHEN expression1a THEN expression1b
CASE is the extension of IF...ELSE
[WHEN expression1a THEN expression1b]
statement. Unlike IF…ELSE,
where only the maximum of one ELSE expressionNb
condition is allowed, CASE allows
the user to apply multiple END
conditions to perform different
sets of actions.
3 3 MS-SQL
4 4 Hadoop
4 4 Hadoop
CASE
WHEN <Boolean_Expression_1> THEN Statement_1
WHEN <Boolean_Expression_2> THEN Statement_2 . .
WHEN <Boolean_Expression_N> THEN Statement_N
Results Messages
[ELSE Statement_Else]
END AS [ALIAS_NAME]
CASE <Case_Expression>
FALSE WHEN Boolean_ Expression_1 THEN Statement_1
WHEN Boolean _Expression_2 THEN Statement_2
Boolean _Expression = TRUE .
Statement_2
Value_2 WHEN Boolean_ Expression_N THEN Statement_N
[ELSE Statement_ELSE]
END
FALSE
Boolean _Expression =
TRUE CASE <Case_Expression>
Statement_N WHEN Boolean_ Expression_1 THEN Statement_1
Value_N
WHEN Boolean_ Expression_2 THEN Statement_2
.
WHEN Boolean_Expression_N THEN Statement_N
FALSE [ELSE Statement_ELSE]
END
Statements_Else
CASE <Case_Expression>
WHEN Boolean_Expression_1 THEN Statement_1
WHEN Boolean_Expression_2 THEN Statement_2
CASE END .
WHEN Boolean_Expression_N THEN Statement_N
[ELSE Statement_ELSE]
END
© BELGIUM CAMPUS 2021
13
Results
• If flight tickets are less than $100, then I will
Messages
Between $100 to $200 Visit New York
visit Los Angeles.
• If flight tickets are between $100 to $200,
then I will visit New York Between $200 to $400 Visit Europe
• If flight tickets are between $200 to $400,
then I will visit Europe
None of the above condition met Nearby tourist spot
• Else, I will prefer to visit some nearby tourist
spot.
Consider below example: None of the above condition met Nearby tourist spot
Results
• If flight tickets are less than $100, then I will
Messages
DECLARE @Flight_Ticket INT
visit Los Angeles.
SET @Flight_Ticket = 190
• If flight tickets are between $100 to $200,
IF @Flight_Ticket >400
then I will visit New York
PRINT ‘Visit Nearby Tourist Location’
• If flight tickets are between $200 to $400,
ELSE
then I will visit Europe
BEGIN
• Else, I will prefer to visit some nearby tourist
SELECT
spot.
CASE
Let's consider categorizing Condition and
WHEN @Flight_Ticket BETWEEN 0 AND 100 THEN ‘Visit LA’
Action separately from the above example:
WHEN @Flight_Ticket BETWEEN 101 AND 200 THEN ‘Visit NY’
WHEN @Flight_Ticket BETWEEN 201 AND 400 THEN ‘Visit Europe’
END AS Location
END