This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
dev:sql:functions:case [2010/07/27 20:03] els |
dev:sql:functions:case [2010/07/27 20:06] els |
||
---|---|---|---|
Line 12: | Line 12: | ||
CASE | CASE | ||
- | WHEN condition_expression_1 THEN scalar_expression_1 | + | WHEN conditional_expression_1 THEN scalar_expression_1 |
- | WHEN condition_expression_2 THEN scalar_expression_2 | + | WHEN conditional_expression_2 THEN scalar_expression_2 |
ELSE scalar_expression | ELSE scalar_expression | ||
END | END | ||
Line 26: | Line 26: | ||
- | == condition_expression == | + | == conditional_expression == |
Required. A conditional expression using SQL language that must prove TRUE in order for the function to return the corresponding scalar expression. A conditional expression is generally equivalent to criteria used in a WHERE clause of a SQL SELECT statement, such as "state in ('CA','OR','WA')" or "birthdate >= 'Jan 1, 1980'. | Required. A conditional expression using SQL language that must prove TRUE in order for the function to return the corresponding scalar expression. A conditional expression is generally equivalent to criteria used in a WHERE clause of a SQL SELECT statement, such as "state in ('CA','OR','WA')" or "birthdate >= 'Jan 1, 1980'. | ||
Line 85: | Line 85: | ||
MR. CHARLIE HOPKINS M 1933-07-09 Mr. | MR. CHARLIE HOPKINS M 1933-07-09 Mr. | ||
MS. KIMBERLY BLUE F 1944-08-01 Ms. | MS. KIMBERLY BLUE F 1944-08-01 Ms. | ||
- | KATHLEEN TUCKER F 2004-09-14 Miss</code> | + | KATHLEEN TUCKER F 2004-09-14 Miss |
</code> | </code> | ||
Line 98: | Line 98: | ||
===== | ===== | ||
- | |||
- | |||
- | |||
- | {{page>:top_add&nofooter&noeditbtn}} | ||
- | <html><div align="center"><span style="color:red">DRAFT</span></div></html> | ||
- | ====== Omnidex SQL Function: CASE ====== | ||
- | {{page>:sql_bar&nofooter&noeditbtn}} | ||
- | ===== Description ===== | ||
- | The CASE function handles simple IF - THEN - ELSE logic at a select-item level. The WHEN conditions are evaluated in order until a condition proves TRUE, at which point the THEN expression is executed. All of the THEN expressions in a CASE statement must be of the same data type: numeric, character, or date. | ||
- | |||
- | CASE supports multiple conditions per case. | ||
- | |||
- | ===== Syntax ===== | ||
- | CASE | ||
- | WHEN condition_expression THEN scalar_expression | ||
- | ELSE scalar_expression | ||
- | END | ||
- | |||
- | or | ||
- | |||
- | CASE scalar_expression_1 | ||
- | WHEN scalar_expression_2 THEN scalar_expression_3 | ||
- | ELSE scalar_expression_4 | ||
- | END | ||
- | |||
- | ===== Example ===== | ||
- | ==== Example 1 Condition Expression ==== | ||
- | <code> | ||
- | SELECT ... | ||
- | CASE | ||
- | WHEN PRODUCTS.CATEGORY =30 THEN 'Furniture' | ||
- | WHEN PRODUCTS.CATEGORY = 70 THEN 'Machines' | ||
- | WHEN PRODUCTS.CATEGORY = 90 THEN 'Paper Products' | ||
- | ELSE 'Office Supplies' | ||
- | END; | ||
- | </code> | ||
- | |||
- | ==== Example 2 Scalar Expression ==== | ||
- | <code SQL> | ||
- | CASE DATES.MO | ||
- | WHEN 'Jan' THEN 1 | ||
- | WHEN 'Feb' THEN 2 | ||
- | WHEN 'Mar' THEN 3 | ||
- | WHEN 'Apr' THEN 4 | ||
- | ELSE 0 | ||
- | END | ||
- | </code> | ||
- | ==== Example 3 Select Statement with Conditional Expression ==== | ||
- | |||
- | <code SQL> | ||
- | select company, | ||
- | |||
- | case | ||
- | when customers.state='CA' then 'CALIFORNIA' | ||
- | when customers.state='CO' then 'COLORADO' | ||
- | when customers.state='NY' then 'NEW YORK' | ||
- | when customers.state='NJ' then 'NEW JERSEY' | ||
- | else 'OTHER' | ||
- | end | ||
- | |||
- | from customers | ||
- | where state in ('ca','co','ny','nj','az','fl','nv') | ||
- | order by state; | ||
- | </code> | ||
- | |||
- | ==== Example 4 Select Statement with Scalar Expression ==== | ||
- | <code SQL> | ||
- | select company, contact, | ||
- | |||
- | case customers.state | ||
- | when 'AZ' then 'ARIZONA' | ||
- | when 'FL' then 'FLORIDA' | ||
- | when 'NV' then 'NEVADA' | ||
- | else 'OTHER' | ||
- | end | ||
- | |||
- | from customers | ||
- | where company='systems' | ||
- | order by state | ||
- | </code> | ||
- | |||
- | {{page>:bottom_add&nofooter&noeditbtn}} |