Tuesday 10 July 2018

Mysql STRCMP() function

Mysql 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
NameDescription
expr1First string for comparison.
expr2Second string for comparison.
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)

0 comments:

Post a Comment