MS Access StrComp() Function
The StrComp() function in MS Access compares two strings and returns a numerical value indicating their relationship. This is useful for sorting, conditional logic, or any situation where you need a programmatic way to compare strings.
StrComp(): Definition and Usage
StrComp() provides a more structured way to compare strings than using operators like = or <>. It returns an integer indicating whether the strings are equal, or which string comes earlier in lexicographical (alphabetical) order. You can also control the type of comparison (binary, text, or database-specific).
Return Values
0: The strings are equal.-1: The first string is less than the second (comes earlier alphabetically).1: The first string is greater than the second (comes later alphabetically).Null: Either or both input strings areNull.
Syntax
Syntax
StrComp(string1, string2, compare)
Parameter Values
| Parameter | Description |
|---|---|
string1, string2 |
The two strings to compare. Both are required. |
compare (Optional) |
The type of comparison:
|
Examples
Comparing Identical Strings
Comparing two strings that are exactly the same.
Syntax
SELECT StrComp("SQL Tutorial", "SQL Tutorial") AS CompString;
Output
0
Comparing Different Strings
Comparing a shorter string to a longer string.
Syntax
SELECT StrComp("SQL Tutorial", "SQL") AS CompString;
Output
1
Comparing Strings in Reverse Order
The reverse comparison.
Syntax
SELECT StrComp("SQL", "SQL Tutorial") AS CompString;
Output
-1