case(X;V1;R1;V2;R2;...;D)

Returns the value corresponding to the case that a given value matches; if no cases match, returns the specified default value.

Syntax

case(X;V1;R1;V2;R2;...;D)

Input

Argument Type Description
X any simple type A scalar value or the name of a column
V[N] any simple type The value (or expression that evaluates to a value) to test for equality to X.

The values specified for all instances of V must be the same type as X.

A scalar value or the name of a column

R[N] any simple type The value to return if V[N] matches X.

The values specified for all instances of R must be the same type (integer, decimal, or text).

A scalar value or the name of a column

D any simple type The default return value (if X doesn't match any of the instances of V).

The value specified for D must be the same type as the values specified for all instances of R.

A scalar value or the name of a column

Note: There must be an even number of arguments (i.e., X, a series of C and R pairs, and one D argument).
   

Return Value

Returns a numeric or text value (depending on the type of the elements specified for R and D) corresponding to the following:
  • If X is V1, return R1; else if X is V2, return R2; and so on.
  • If X does not match any of the values specified by V[N], return D.

If any of the values corresponding to C[N] are N/A, an error is returned.

Sample Usage

X V1 R1 V2 R2 V3 R3 D case(X;V1;R1;V2;R2;V3;R3;D)
2 1 'one' 2 'two' 3 'three' 'too many' 'two'
99 1 'one' 2 'two' 3 'three' 'too many' 'too many'

Example

Let's use the table pub.demo.retail.item to illustrate the use of case(X;V1;R1;V2;R2;...;D).

Let's say we have three stores and that store 1 is located in New York, store 2 is located in Miami, and store 3 is in London. To add a column to our table that corresponds to the location for each store, we can use the following Macro Language code:
<willbe name="location" value="case(store;1;'New York';2;'Miami';3;'London';'Not a valid location')"/>

This will give us the following new column:

Since, in our example, there are no values in the store column that are not 1, 2, or 3, the default value ('Not a valid location') is never used.