I'm a professional writer and Business Development with more than 10 years of experience. I have worked for a lot of businesses and can share sample works with you upon request. Chat me up and let's get started.
Where A CASE is the special scalar expression or conditional statement in the SQL language which returns a single value based on the evaluation of a statement. The Case statements can be used in Select and Where clauses and even an Order By clause. The Case expression is mostly used in SQL stored procedures or as a formula for a particular column, which optimizes the SQL statements.
We'll start by walking through a simple case statement. The Case statement can be used in two forms in SQL Server:
Case statement with a simple expression.
Case statement with the comparison or searched expression.
Case statement with the simple expression
A Simple Case statement checks only for equivalent values and cannot contain Boolean expressions. The Simple Case statement looks for the first expression in the list of all when the clause that matches statement_1 and evaluates the corresponding when clause. Whether there is no match, then the else clause is evaluated.
The general syntax of the Simple CASE expression is: CASE expression
WHEN exp_1 THEN result_1
[WHEN exp_2 THEN result_2]
[..................]
[WHEN exp_n THEN result_n]
[ELSE expression]
END
CASE is used to provide the if-then-else type of logic to SQL. There are two formats: The
first is a Simple CASE expression, where we compare an expression to static values. The
second is a Searched CASE expression, where we compare an expression to one or more logical conditions.
Simple CASE Expression Syntax
The syntax for a simple CASE expression is:
SELECT CASE ("column_name")
WHEN "value1" THEN "result1"
WHEN "value2" THEN "result2"
...
[ELSE "resultN"]
END
FROM "table_name";
The ELSE clause is optional.
Simple CASE Expression Example
We use the following table for our example.
Table Store_Information
Liked By
Write Answer
What is the use of case in sql server ?
Join MindStick Community
You have need login or register for voting of answers or question.
Sarah Alfred
17-Apr-2019Very well described. Thanks for sharing it with the community
Anonymous User
11-Apr-2019Where A CASE is the special scalar expression or conditional statement in the SQL language which returns a single value based on the evaluation of a statement. The Case statements can be used in Select and Where clauses and even an Order By clause. The Case expression is mostly used in SQL stored procedures or as a formula for a particular column, which optimizes the SQL statements.
We'll start by walking through a simple case statement. The Case statement can be used in two forms in SQL Server:
A Simple Case statement checks only for equivalent values and cannot contain Boolean expressions. The Simple Case statement looks for the first expression in the list of all when the clause that matches statement_1 and evaluates the corresponding when clause. Whether there is no match, then the else clause is evaluated.
Rahul Roi
11-Apr-2019CASE is used to provide the if-then-else type of logic to SQL. There are two formats: The first is a Simple CASE expression, where we compare an expression to static values. The second is a Searched CASE expression, where we compare an expression to one or more logical conditions.