w3resource
MYSQL Tutorial

MySQL STRCMP() function

STRCMP() function

MySQL strcmp() function is used to compare two strings. It returns 0 if both of the strings are same and returns -1 when the first argument is smaller than the second according to the defined order and 1 when the second one is smaller the first one.

Syntax:

STRCMP (expr1, expr2)

Argument

Name Description
expr1 First string for comparison.
expr2 Second string for comparison.

Syntax Diagram:

MySQL STRCMP() Function - Syntax Diagram

MySQL Version: 5.6

Video Presentation

Example : MySQL STRCMP() function

In the MySQL statement stated below, using the STRCMP() function, two strings are compared, and since it is found that the strings are same, it returns 0.

Code:

SELECT STRCMP('mytesttext', 'mytesttext');

Sample Output:

mysql> SELECT STRCMP('mytesttext', 'mytesttext');
+------------------------------------+
| STRCMP('mytesttext', 'mytesttext') |
+------------------------------------+
|                                  0 | 
+------------------------------------+
1 row in set (0.01 sec)

MySQL STRCMP() function with unmatched strings

The MySQL statement stated below, using the STRCMP() function to compare two strings and returns -1 according to the default comparison behavior

Code:

SELECT STRCMP('mytesttext', 'mytest_text');

Sample Output:

mysql> SELECT STRCMP('mytesttext', 'mytest_text');
+-------------------------------------+
| STRCMP('mytesttext', 'mytest_text') |
+-------------------------------------+
|                                  -1 | 
+-------------------------------------+
1 row in set (0.00 sec)

All String Functions

MySQL String Functions, slide presentation