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