MariaDB Tutorial MariaDB Advanced MariaDB Database Account Management MariaDB References

MariaDB STRCMP() Function



The MariaDB STRCMP() function checks whether two strings are the same using the current character set. Based on the value of string1 and string2, the function returns the following:

  • Returns -1, if string1 is smaller than string2.
  • Returns 0, if string1 and string2 are the same.
  • Returns 1, if string1 is larger than string2.

Syntax

STRCMP(string1, string2)

Parameters

string1 Required. Specify the first string to be compared.
string2 Required. Specify the second string to be compared.

Return Value

Returns the following value:

  • Returns -1, if string1 is smaller than string2.
  • Returns 0, if string1 and string2 are the same.
  • Returns 1, if string1 is larger than string2.

Example 1:

The example below shows the usage of STRCMP() function.

SELECT STRCMP('Hello', 'Hello');
Result: 0

SELECT STRCMP('Hello', 'World');
Result: -1

SELECT STRCMP('World', 'Hello');
Result: 1

SELECT STRCMP('HELLO', 'hello');
Result: 0

Example 2:

Consider a database table called Employee with the following records:

EmpIDFirstNameLastName
1JohnSmith
2MarryKnight
3JoWilliams
4KimFischer
5RameshGupta
6HuangZhang

In the query below, the STRCMP() function is used to compare the records of column FirstName and column LastName.

SELECT *, STRCMP(FirstName, LastName) AS STRCMP_Value FROM Employee;

This will produce the result as shown below:

EmpIDFirstNameLastNameSTRCMP_Value
1JohnSmith-1
2MarryKnight1
3JoWilliams-1
4KimFischer1
5RameshGupta1
6HuangZhang-1

❮ MariaDB Functions