CodesJava

Easy learning with example program codes

what is the sql case statement used for?


SQL Case Statement

Switch statement is used to execute a block of statement based on the switch expression value. An expression must be of type int, short, byte or char. A case value should be a constant literal value and cannot be duplicated. Expression value is compared with each case value. If a match found corresponding block of statements will be executed. A break statement is used to terminate the execution of statements. If no case value matches with expression value then default block of statements will be executed. If break statement is not used within case, all matching cases will be executed.

Syntax

CASE expression
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
     ...
    WHEN conditionN THEN resultN
    ELSE result
END

Example

SELECT CustomerID, CustomerName, "Result"=
CASE
    WHEN CustomerID > 10 THEN "The customerId is greater than 10"
    WHEN CustomerID = 10 THEN "The customerId is 10"
    ELSE "The customerId is something else"
END

Related topics

Posted in SQL   
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki