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

Many of our valued users post comments along with piece of code. Disqus may, false positively, consider those piece of code as Bad or Starnge syntax and send those comments to spam automatically. Even if that happens, we will make sure those useful comments are taken out of spam and approved. It may take a couple of days for that though, but any useful comment will be brought to public view for sure. We regret if that happend to any user. You may write us directly regarding this to - w3resource[at]yahoo[dot]com.