This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
dev:sql:functions:case [2009/12/04 22:20] tdo created |
dev:sql:functions:case [2012/10/26 14:28] (current) |
||
|---|---|---|---|
| Line 2: | Line 2: | ||
| <html><div align="center"><span style="color:red">DRAFT</span></div></html> | <html><div align="center"><span style="color:red">DRAFT</span></div></html> | ||
| ====== Omnidex SQL Function: CASE ====== | ====== Omnidex SQL Function: CASE ====== | ||
| + | {{page>:sql_bar&nofooter&noeditbtn}} | ||
| ===== Description ===== | ===== 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. | 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. | ||
| Line 11: | Line 12: | ||
| END | END | ||
| + | or | ||
| + | |||
| + | CASE scalar_expression_1 | ||
| + | WHEN scalar_expression_2 THEN scalar_expression_3 | ||
| + | ELSE scalar_expression_4 | ||
| + | END | ||
| ===== Example ===== | ===== Example ===== | ||
| - | ==== Example 1 ==== | + | ==== Example 1 Condition Expression ==== |
| - | ==== Example 2 ==== | + | <code SQL> |
| + | 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}} | {{page>:bottom_add&nofooter&noeditbtn}} | ||