iif

Syntax

iif(cond, trueResult, falseResult)

Arguments

cond is a Boolean scalar/vector/matrix. It can be an expression returning Boolean values.

trueResult and falseResult are scalars, or vectors of the same length as cond, or matrices of the same dimensions as cond.

Details

Element-wise conditional operation. cond is of Boolean type. If cond[i] is true, it returns trueResult(cond[i] ); otherwise it returns falseResult(cond[i] ). If cond[i] is NULL, it returns NULL.

Examples

$ iif(true true true false false false, 1..6, 6..1);
[1,2,3,3,2,1]

$ iif(1..6==3, 1, 2);
[2,2,1,2,2,2]

$ x=9 6 8;
$ iif(x<=8, 10*x, 20*x-80);
[100,60,80]

Use function iif in a SQL statement:

$ t=table(1..5 as id, 11..15 as x);
$ t1=table(take(12,5) as a, take(14,5) as b);
$ t;

id

x

1

11

2

12

3

13

4

14

5

15

$ t1;

a

b

12

14

12

14

12

14

12

14

12

14

$ update t set x=iif(x<t1.a, t1.a, iif(x>t1.b,t1.b, x));
$ t;

id

x

1

12

2

12

3

13

4

14

5

14

$ a = NULL 1 -3 5
$ iif(a > 0, a, 0)
[0, 1, 0, 5]
$ iif(nullCompare(>,a,0), a, 0)
[ , 1, 0, 5]
$ m1=1..6$3:2
$ m2=6..1$3:2
$ iif(m1>m2, m1, m2);

col1

col2

6

4

5

5

4

6