Friday, October 12, 2018

Sample SQL function CASE in ODBC

Image


The CASE SQL function is similar to an If-Then-Else statement. It specifies a series of search conditions and associated result expressions. The general form is called a searched case expression. SQL returns the value specified by the first result expression whose associated search condition evaluates as true. If none of the search conditions evaluates as true, the CASE expression returns a null value, or the value of some other default expression if the CASE expression includes the ELSE clause.
 
SYNTAX
CASE
  WHEN primary_expr = expr1 THEN result_expr1
  WHEN primary_expr = expr2 THEN result_expr2
  ELSE expr3
END
 
EXAMPLE
SELECT COMPANYNAME, ZIPCODE,
             CASE
               WHEN ZIPCODE = '94103'  THEN  'CALIFORNIA'
               WHEN ZIPCODE = '85255' THEN  'ARIZONA'
               ELSE 'NO STATE'
             END
FROM CUSTOMERS;

 

No comments:

Post a Comment