The STRCMP SQL Function in MySQL and MariaDB - Compare strings |
|
| STRCMP | Syntax: | STRCMP(String 1, String 2) | Return value: | INTEGER | Function type: | Comparison function | |
| | The STRCMP() function compares two strings.
If the strings are equal, the function returns 0.
If "String 1" is greater than "String 2", the function returns 1.
If "String 1" is less than "String 2", the function returns -1.
Numerical values are interpreted as character strings.
If one or both of the arguments are NULL, STRCMP() returns NULL. | SQL Examples for the STRCMP function |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
|
SELECT strcmp('abc', 'abc');
SELECT strcmp('abc', 'bcd');
SELECT strcmp('abc', 'abc2');
SELECT strcmp('abc2', 'abc');
SELECT strcmp(1, 1);
SELECT strcmp(1, 2);
SELECT strcmp(2, 1);
SELECT strcmp(22, 3);
SELECT strcmp(22, 2);
SELECT strcmp(22, 22.0);
SELECT strcmp(false, true);
SELECT strcmp(true, false);
SELECT strcmp(null, 'abc');
SELECT strcmp('abc', null);
SELECT strcmp(null, null);
|
|
strcmp('abc', 'abc') |
bigint(2) |
0 |
|
|
strcmp('abc', 'bcd') |
bigint(2) |
-1 |
|
|
strcmp('abc', 'abc2') |
bigint(2) |
-1 |
|
|
strcmp('abc2', 'abc') |
bigint(2) |
1 |
|
|
|
strcmp(1, 2) |
bigint(2) |
-1 |
|
|
|
strcmp(22, 3) |
bigint(2) |
-1 |
|
|
strcmp(22, 2) |
bigint(2) |
1 |
|
|
strcmp(22, 22.0) |
bigint(2) |
-1 |
|
|
strcmp(false, true) |
bigint(2) |
-1 |
|
|
strcmp(true, false) |
bigint(2) |
1 |
|
|
strcmp(null, 'abc') |
bigint(2) |
NULL |
|
|
strcmp('abc', null) |
bigint(2) |
NULL |
|
|
strcmp(null, null) |
bigint(2) |
NULL |
|
|
| The examples were created with the MyWAY SQL manager: | How to use the STRCMP() function in MySQL and MariaDB databases | In MySQL and MariaDB the STRCMP() function can be used to compare two strings and get an integer value representing the result of the comparison, which can be used to perform case-sensitive string comparisons, such as for conditional statements or sorting operations of SQL queries in databases. | | Further MySQL und MariaDB SQL Comparison functions | |
| | More information about the STRCMP SQL function: and and |
|
|
|
|