CASE IN SQL

HOW TO USE 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;


Posted

in

Tags:

Comments

Leave a Reply