This shows you the differences between two versions of the page.
dev:sql:functions:case [2009/12/04 22:23] tdo |
dev:sql:functions:case [2016/06/28 22:38] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | {{page>:top_add&nofooter&noeditbtn}} | ||
- | <html><div align="center"><span style="color:red">DRAFT</span></div></html> | ||
- | ====== Omnidex SQL Function: CASE ====== | ||
- | ===== 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. | ||
- | ===== 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 ==== | ||
- | CASE | ||
- | WHEN PRODUCTS.CATEGORY < 30 THEN 'Furniture' | ||
- | WHEN PRODUCTS.CATEGORY < 70 THEN 'Machines' | ||
- | WHEN PRODUCTS.CATEGORY < 90 THEN 'Paper Products' | ||
- | ELSE 'Office Supplies' | ||
- | END | ||
- | |||
- | ==== Example 2 ==== | ||
- | CASE DATES.MO | ||
- | WHEN 'Jan' THEN 1 | ||
- | WHEN 'Feb' THEN 2 | ||
- | WHEN 'Mar' THEN 3 | ||
- | WHEN 'Apr' THEN 4 | ||
- | ELSE 0 | ||
- | END | ||
- | |||
- | ==== Example 3 ==== | ||
- | |||
- | <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 ==== | ||
- | <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}} |