Operators¶
An operator combines two values into a result. In 4 + 5 the values 4 and 5 are the
operands and + is the operator.
- Arithmetic operators
- Comparison operators
- Logical operators
- Concatenation operators
- The order operators are applied in
Arithmetic operators¶
With A holding 5 and B holding 10:
| Operator | Does | Example | Result |
|---|---|---|---|
+ |
Adds | A + B |
15 |
- |
Subtracts the second from the first | A - B |
-5 |
* |
Multiplies | A * B |
50 |
/ |
Divides | B / A |
2 |
\ |
Divides and discards the remainder | B \ 4 |
2 |
Mod |
Gives the remainder of a division | B Mod A |
0 |
^ |
Raises to a power | B ^ A |
100000 |
/ keeps the fractional part and \ does not. 10 / 4 is 2.5 and 10 \ 4 is 2.
The remainder operator is Mod, not a per cent sign
Some languages write the remainder as %. VBScript writes it as Mod. In IMan a
per cent sign starts a
field reference.
%Qty reads a field and never divides anything.
Comparison operators¶
A comparison returns True or False. With A holding 10 and B holding 20:
| Operator | True when | Example | Result |
|---|---|---|---|
= |
The two are equal | A = B |
False |
<> |
The two differ | A <> B |
True |
> |
The left is greater | A > B |
False |
< |
The left is smaller | A < B |
True |
>= |
The left is greater or equal | A >= B |
False |
<= |
The left is smaller or equal | A <= B |
True |
A Filter transform's expression is a comparison, because a Filter needs True or False:
Two numbers held as text compare alphabetically
VBScript compares two strings character by character and two numbers by value.
"10" > "9" is False, because 1 sorts before 9. 10 > 9 is True, and so
is "10" > 9. One number among the operands makes it a numeric comparison.
A field typed as Text holds a string, whatever its characters. Two Text fields compared with each other therefore compare alphabetically. The CSV and Excel readers type a column of numbers as a number, but a single value with a leading zero forces the column to Text. Convert both sides where the fields hold numbers:
CDbl raises a type mismatch on a value that is not numeric. A comparison of a
non-numeric string against a number raises one too. Test with IsNumeric where
the data is not trustworthy.
A comparison against Null is never True. Null = Null is Null, not True. See
Empty and Null are different things.
Logical operators¶
A logical operator combines two comparisons. With A holding 10 and B holding 0:
| Operator | True when | Example | Result |
|---|---|---|---|
And |
Both sides are True | A <> 0 And B <> 0 |
False |
Or |
Either side is True | A <> 0 Or B <> 0 |
True |
Not |
The one operand is False | Not (A <> 0) |
False |
Xor |
Exactly one side is True | A <> 0 Xor B <> 0 |
True |
Eqv |
Both sides are the same | A <> 0 Eqv B <> 0 |
False |
Imp |
Anything but a True left side and a False right side | A <> 0 Imp B <> 0 |
False |
With %Qty of 5 and %WarehouseCode of MAIN, the expression returns True.
Concatenation operators¶
Concatenation joins two values into one string. VBScript has two operators for it, and they behave differently.
& always concatenates. It converts each operand to a string first, whatever the
operand holds:
With %ItemCode of IC-1000 and %Qty of 5, the result is IC-1000 x 5.
+ concatenates only when both operands are strings. Given two numbers it adds
them. Given a number and a string it tries to convert the string to a number:
| Expression | Result |
|---|---|
"five" + "ten" |
fiveten |
5 + 10 |
15 |
5 + 10 + "3" |
18 |
5 + 10 + "three" |
Error 13, "Type mismatch" |
Use the ampersand for concatenation, not the plus
+ gives a different answer depending on what its operands happen to hold, and it
fails outright on a number plus a non-numeric string. A field holding 3 on most
records and three on one will run for months and then stop. & converts both
sides and joins them. 5 & 10 & "three" returns 510three in place of failing.
& also handles a Null, where the arithmetic operators do not. Null & "x" is
"x", and Null + 1 is Null.
The order operators are applied in¶
VBScript applies arithmetic first, then concatenation, then comparison, then logical:
^*and/\Mod+and-&=,<>,<,>,<=,>=Not, thenAnd, thenOr, thenXor, thenEqv, thenImp
2 + 3 * 4 is 14 rather than 20, and 1 + 2 & "3" is 33 rather than 123. Brackets
override the order. Write them wherever the order is not obvious at a glance: