0% found this document useful (0 votes)
41 views

© Belgium Campus 2021

Uploaded by

Enrico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

© Belgium Campus 2021

Uploaded by

Enrico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1

©©BELGIUM
BELGIUMCAMPUS
CAMPUS2021
2021
2

© BELGIUM CAMPUS 2021


3

CASE <Case_Expression> If the <Case_Expression> is


equal to Value_1, then
WHEN Value_1 THEN Statement_1 Statement_1 is executed.

CASE is the extension of IF...ELSE WHEN Value_2 THEN Statement_2


statement. Unlike IF…ELSE,
where only the maximum of one
.
condition is allowed, CASE allows
the user to apply multiple
.
conditions to perform different After Statement_1 is
sets of actions.
WHEN Value_N THEN Statement_N executed, all the other
statements are skipped and
[ELSE Statement_ELSE] the CASE ends.

END

© BELGIUM CAMPUS 2021


4

CASE <Case_Expression> If the <Case_Expression> is NOT equal to


Value_1, then Statement_1 is skipped, and
WHEN Value_1 THEN Statement_1 the next value is checked.

CASE is the extension of IF...ELSE WHEN Value_2 THEN Statement_2


statement. Unlike IF…ELSE,
where only the maximum of one
.
condition is allowed, CASE allows
the user to apply multiple
.
conditions to perform different If the <Case_Expression> is equal to Value_2,
sets of actions.
WHEN Value_N THEN Statement_N then Statement_2 is executed and everything
else is skipped.
[ELSE Statement_ELSE] A CASE statement goes through all its conditions
and ONLY executes the one that is true.
END

© BELGIUM CAMPUS 2021


CASE <Case_Expression> 5
WHEN Value_1 THEN Statement_1
START – CASE Keyword WHEN Value_2 THEN Statement_2
.
WHEN Value_N THEN Statement_N
TRUE [ELSE Statement_ELSE]
CASE_Expression =
Statement_1 END
Value_1

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.

© BELGIUM CAMPUS 2021


7

DECLARE @IntVar INT


SET @IntVar = 1
Here is an example of using a
SELECT CASE @IntVar
CASE expression in a SELECT
statement: WHEN 0 THEN ‘This won’t show’
WHEN 1 THEN ‘This will show’
ELSE ‘This won’t show’
END

© BELGIUM CAMPUS 2021


8

SELECT Tutorial_ID, Tutorial_name,

Assume that we have the table


CASE Tutorial_name
as ’Gur99' with two columns and WHEN ‘SQL’ THEN ‘SQL is developed by IBM’
four rows as displayed below:
WHEN ’PL/SQL’ THEN ‘PL/SQL is developed by Oracle Corporation.’
Results Messages
WHEN ‘MS-SQL’ THEN ‘MS-SQL is developed by Microsoft Corporation’
Tutorial_ID Tutorial_name
END AS Description
1 1 SQL
FROM Guru99
2 2 PL/SQL

3 3 MS-SQL

4 4 Hadoop

© BELGIUM CAMPUS 2021


Source https://www.guru99.com/sql-server-case.html
9

© BELGIUM CAMPUS 2021


Source https://www.guru99.com/sql-server-case.html
10

SELECT Tutorial_ID, Tutorial_name,

Assume that we have the table


CASE Tutorial_name
as ’Gur99' with two columns and WHEN ‘SQL’ THEN ‘SQL is developed by IBM’
four rows as displayed below:
WHEN ’PL/SQL’ THEN ‘PL/SQL is developed by Oracle Corporation.’
Results Messages
WHEN ‘MS-SQL’ THEN ‘MS-SQL is developed by Microsoft Corporation’
Tutorial_ID Tutorial_name
ELSE ‘This is NO SQL language.’
1 1 SQL
END AS Description
2 2 PL/SQL
FROM Guru99
3 3 MS-SQL

4 4 Hadoop

© BELGIUM CAMPUS 2021


Source https://www.guru99.com/sql-server-case.html
11

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]

© BELGIUM CAMPUS 2021


Source https://www.guru99.com/sql-server-case.html
CASE <Case_Expression> 12
WHEN Boolean_Expression_1 THEN Statement_1
START – CASE Keyword WHEN Boolean_Expression_2 THEN Statement_2
.
WHEN Boolean_ Expression_N THEN Statement_N
TRUE [ELSE Statement_ELSE]
Boolean_Expression =
Statement_1 END
Value_1

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

Conditions – Flight Tickets Actions performed, only if Condition is TRUE

Consider below example: Less than $100 Visit Los Angeles

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.

Let's consider categorizing Condition and


Action separately from the above example:

© BELGIUM CAMPUS 2021


14

Conditions – Flight Tickets Actions performed, only if Condition is TRUE

Less than $100 Visit Los Angeles

Between $100 to $200 Visit New York

Between $200 to $400 Visit Europe

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

© BELGIUM CAMPUS 2021

You might also like