STRCMP
Descriptionβ
The STRCMP function compares two strings lexicographically. It returns an integer value indicating the result of the comparison.
Syntaxβ
STRCMP(<str0>, <str1>)
Parametersβ
Parameter | Description |
---|---|
<str0> | The first string to compare. Type: VARCHAR |
<str1> | The second string to compare. Type: VARCHAR |
Return Valueβ
Returns a TINYINT value indicating the comparison result:
- Returns 0: if str0 equals str1
- Returns 1: if str0 is lexicographically greater than str1
- Returns -1: if str0 is lexicographically less than str1
Special cases:
- Returns NULL if any argument is NULL
Examplesβ
- Comparing identical strings
SELECT strcmp('test', 'test');
+------------------------+
| strcmp('test', 'test') |
+------------------------+
| 0 |
+------------------------+
- First string is greater
SELECT strcmp('test1', 'test');
+-------------------------+
| strcmp('test1', 'test') |
+-------------------------+
| 1 |
+-------------------------+
- First string is smaller
SELECT strcmp('test', 'test1');
+-------------------------+
| strcmp('test', 'test1') |
+-------------------------+
| -1 |
+-------------------------+