Comparison operators
Topic group Related topics Example
Comparison operators compare two expressions. The comparison returns a logical true or false value. Comparing logical expressions is allowed, but redundant; use logical operators instead.
dBASE Plus automatically converts data types in a comparison, using the following rules:
If the two operands are the same type, they are compared as-is.
If either operand is a numeric expression, the other operand is converted to a number:
If a string contains a number only (leading spaces are OK), that number is used, otherwise it is interpreted as an invalid number.
The logical value true becomes one; false becomes zero.
All other data types are invalid numbers.
All comparisons between a number and an invalid number result in false.
If either operand is a string, the other operand is converted to its display representation:
Numbers become strings with no leading spaces. Integer values eight digits or less have no decimal point or decimal portion. Integer values larger than eight digits and non-integer values have as many decimals places as indicated by SET DECIMALS.
The logical values true and false become the strings "true" and "false".
Dates (primitive dates and Date objects) are converted using DTOC( ).
Object references to arrays are converted to the word "Array".
References to objects of all other classes are converted to the word "Object".
Function pointers take on the form "Function: " followed by the function name.
All other comparisons between mismatched data types return false.
These are the comparison operators:
Operator |
Description |
== |
Exactly equal to |
= |
Equal to or Begins with |
<> or # |
Not equal to or Doesn’t begin with |
< |
Less than |
> |
Greater than |
<= |
Less than or equal to |
>= |
Greater than or equal to |
$ |
Contained in |
When comparing dates, a blank date comes after (is greater than) a non-blank date.
When comparing Date objects, the date/time they represent are compared; they may be earlier, later, or exactly the same. For all other objects, only the equality tests makes sense. It tests whether two object references refer to the same object.
String equality comparisons are case-sensitive and follow the rules of SET EXACT. The == operator always compares two strings as if SET EXACT is ON. The other equality operators (=, <>, #) use the current setting of SET EXACT. When SET EXACT is ON, trailing blanks in either string are ignored in the comparison. When SET EXACT is OFF (the default), the = operator act like a "begins with" operator: the string on the left must begin with the string on the right. The <> and # operators act like "does not begin with" operators. Note that there is no single genuinely exactly equal comparison for strings in dBASE Plus.
It is recommended that you leave SET EXACT OFF so that you have the flexibility of doing an "exact" comparison or a "begins with" comparison as needed. By definition, all strings "begin with" an empty string, so when checking if a string is empty, always put the empty string on the left of the equality operator.
Warning!
For compatibility with earlier versions of dBASE, if the string on the right of the = operator is (or begins with) CHR(0) and SET EXACT is OFF, then the comparison always returns true. When checking for CHR(0), always use the == operator.
The $ operator determines if one string is contained in, or is a substring of, another string. By definition, an empty string is not contained in another string.