MySQL STRCMP() Function
The STRCMP()
function in MySQL compares two strings and returns a numerical value indicating their relative order.
STRCMP(): Definition and Usage
This function is useful for sorting, filtering, or performing conditional logic based on string comparisons. It provides a more programmatic way to handle string comparisons compared to using operators like =
or !=
.
Return Values
STRCMP()
returns:
0
if the strings are equal.-1
if the first string is lexicographically less than the second string (comes before it alphabetically).1
if the first string is lexicographically greater than the second string (comes after it alphabetically).
Syntax
Syntax
STRCMP(string1, string2)
Parameter Values
Parameter | Description |
---|---|
string1, string2 |
The two strings to compare. Both are required. |
Examples
Comparing Identical Strings
Comparing two identical strings.
Syntax
SELECT STRCMP("SQL Tutorial", "SQL Tutorial");
Output
0
Comparing Different Strings
Comparing two different strings.
Syntax
SELECT STRCMP("SQL Tutorial", "HTML Tutorial");
Output
1