IIF( ) Function

Returns one of two values depending on the value of a logical expression.

IIF(lExpression, eExpression1, eExpression2)

Return Values

Character, Numeric, Currency, Date, or DateTime

Parameters

  • lExpression
    Specifies the logical expression that IIF( ) evaluates.
  • eExpression1, eExpression2
    If lExpression evaluates to True (.T.), eExpression1 is returned and eExpression2 is not evaluated. If lExpression evaluates to False (.F.) or null (.NULL.), eExpression2 is returned and eExpression1 is not evaluated.

Remarks

This function, also known as Immediate IF, evaluates a logical expression and then returns one of two expressions. If the logical expression evaluates to True (.T.), IIF( ) returns the first expression. If the logical expression evaluates to False (.F.) or null (.NULL.), IIF( ) returns the second expression.

Tip   This function can be used in place of IF ... ENDIF for simple conditional expressions, and is especially useful in report and label expressions that conditionally specify field contents. The IIF( ) function also executes faster than an equivalent IF ... ENDIF.

Example

The following example uses IIF( ) to check if the notes field in the employee table is empty. If it is empty, "No description" is displayed; otherwise, the contents of the memo field are displayed.

CLOSE DATABASES
OPEN DATABASE (HOME(2) + 'Data\testdata')
USE employee  && Open Employee table
CLEAR

SCAN
   ? IIF(EMPTY(notes), 'No notes', notes)    && Empty memo field?
ENDSCAN

See Also

IF ... ENDIF | #IF ... #ENDIF Preprocessor Directive | IF Statements