HOW TO USE CASE IN SQL

CASE IN SQL

CASE statement is used to return the value if the specified condition evaluates to True, else it returns the value of the ELSE part. When there is no ELSE block and no condition evaluates to True, it returns a NULL value.

Syntax of CASE in SQL-

CASE

  WHEN <condition1> THEN <result1>…….

  WHEN <conditionN> THEN <resultN>

  ELSE <result>

END;

Example of CASE in SQL-It will returns a value when the first condition is met-

SELECT OrderID, Quantity,

CASE

  WHEN Quantity > 20 THEN  ‘Quantity is greater than 20’

  WHEN Quantity >=20 THEN  ‘Quantity is equak to 20’

  ELSE ‘Quantity is less than 20’

END

AS QuantityDetails

FROM Order Derails;


Leave a Reply

Your email address will not be published. Required fields are marked *