Sql Server

SQL | Case Statement

SQL | Case Statement

Case Statement in Sql Server

CASE expression in Sql Server
SQL Server CASE expression evaluates a list of conditions and returns a value when the first condition is met (like an if-then-else statement).
Once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause.
It will give NULL value when no ELSE block is found and no conditions are met true.


Simple CASE statement:

CASE <input_expression>  
     WHEN Condition1 THEN Statement1  
     WHEN Condition2 THEN Statement2  
     .  
     .  
     WHEN ConditionN THEN StatementN  
     ELSE Statement    
END  

SELECT stateId, stateCode,  
CASE stateCode
    WHEN 'UP' THEN 'Uttar Pradesh'  
    WHEN 'DL' THEN 'New Delhi'  
    WHEN 'PB' THEN 'Punjab'  
    ELSE 'Do not Know.'  
END AS Description  
FROM States;



Searched CASE Statement
evaluates a set of Boolean expressions to find the result

CASE  
  WHEN boolean_expression_1 THEN result_1  
  WHEN boolean_expression_2 THEN result_2  
  .  
  .  
  WHEN boolean_expression_N THEN result_N  
  ELSE else_result  
END;  


SELECT Id, name, age,  
CASE  
WHEN age>60 THEN 'Senior'
 WHEN age>=40 AND age<60 THEN 'Mature'
 WHEN age>=20 AND age<40 THEN 'Adult'
 WHEN age>=13 AND age<20 THEN 'Teen'
 WHEN age>=5 AND age<13 THEN 'Child'
 WHEN age>2 AND age<5 THEN 'Toddler'  
 ELSE 'Infant'  
END AS Maturity FROM User;  




Nested CASE Statement

SELECT Id, name, salary,  
CASE  
 WHEN salary>25000 THEN  
  CASE  
   WHEN salary> 35000 THEN 'General Manager'  
   ELSE 'Manager'  
  END  
 ELSE 'Assistant Manager'  
END AS Designation  
FROM Emmployee;  




CASE Statement with ORDER BY Clause
We can also use a CASE statement with an ORDER BY clause, which is used to sort the results in ascending or descending order.

SELECT Id, name, age, salary  
FROM STUDENT  
ORDER BY
  CASE  
    WHEN age>17 THEN salary End DESC,  
    CASE WHEN age<17 THEN salary    
  END



Case Statement in SQL with Group by clause

Select
  CASE
    WHEN Salary >=80000 AND Salary <=100000 THEN 'Director'
    WHEN Salary >=50000 AND Salary <80000 THEN 'Senior Consultant'
    Else 'Director'
  END AS Designation,
  Min(salary) as MinimumSalary,
  Max(Salary) as MaximumSalary
from Employee
Group By
  CASE
    WHEN Salary >=80000 AND Salary <=100000 THEN 'Director'
    WHEN Salary >=50000 AND Salary <80000 THEN 'Senior Consultant'
    Else 'Director'
END




UPDATE Statement with a CASE Statement
SQL Server also allows us to use the CASE statement with the UPDATE statement. Suppose we want to update student's ages based on CASE statement conditions.

UPDATE STUDENT  
SET age =
  CASE age  
    WHEN 22 THEN 23  
    WHEN 17 THEN 18  
    WHEN 29 THEN 30  
    WHEN 16 THEN 15  
    ELSE 25  
END  



Insert statement with CASE statement

Insert into employee
values
(
    @EmployeeName,
      CASE @Gender
      WHEN 0 THEN 'M'
      WHEN 1 THEN 'F'
      end,
    @Statecode,
    @salary
)



Difference Between Simple and Searched Case

Simple CASESearched CASE
There is an expression between the CASE keyword and WHEN clause. For example,
CASE
WHEN Condition1 THEN Statement1 There is no expression between the CASE keyword and WHEN clause. For example,
CASE WHEN Condition1 THEN Statement1
This statement is used for a simple equality check and determines the result by comparing an expression to a set of multiple values.This statement tests the conditions for each of the "when" statements separately. It helps us to solve more complex conditions than a simple CASE.
The simple CASE statement only supports equality tests.The searched CASE statement supports any operation that returns a Boolean value with Boolean_Expression. It includes the equal and not equal to operators.



Limitations of CASE Statement
The CASE statement does not allow us to control the execution flow of stored procedures and functions in SQL Server.
The CASE statement can have several conditions in a Case statement, but it operates only in a sequential model. When one of the conditions becomes true, it stops testing further statements.
The CASE statement does not allow us to NULL values in a table.










Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram