SQL CASE. Exemple SQL CASE.

Exemple SQL CASE

Următorul SQL trece prin condiții și returnează o valoare atunci când este îndeplinită prima condiție:

Exemplu
 
SELECT OrderID, Quantity,
CASE
    WHEN Quantity > 30 THEN "The quantity is greater than 30"
    WHEN Quantity = 30 THEN "The quantity is 30"
    ELSE "The quantity is under 30"
END AS QuantityText
FROM OrderDetails;

Următorul SQL va ordona (order) clienții (customers) după oraș. Cu toate acestea, dacă orașul este NULL, atunci ordonați (order) după țară:

Exemplu:
 
SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
    WHEN City IS NULL THEN Country
    ELSE City
END);