STRCMP() Function in MySQL
Last Updated :
11 Jan, 2021
STRCMP() function in MySQL is used to compare two strings. If both of the strings are same then it returns 0, if the first argument is smaller than the second according to the defined order it returns -1 and it returns 1 when the second one is smaller the first one.
Syntax : STRCMP(Str1, Str2)
Parameter : This method accepts two-parameter as described below :
-
str1 : It is the first string used for comparison.
-
str2 : It is the second string used for comparison.
Returns : It can give four kinds of value –
- If string1 = string2, this function returns 0
- If string1 < string2, this function returns -1
- If string1 > string2, this function returns 1
- If any one or both string is NULL, this function returns NULL .
Example-1 : STRCMP() function to compare two equal string. As both given strings are equal it will return 0.
Select STRCMP('Geeks', 'Geeks') As 'Cmp_Value'
Output :
Example-2 : STRCMP() function to compare two string when second string is smaller than the first string. Here, the return value will be 1.
Select STRCMP('Geeks', 'Geek') As 'Cmp_Value'
Output :
Example-3 : STRCMP() function to compare two string when second string is bigger than the first string. As the second string is greater than the first one the result will be -1.
Select STRCMP('Geek', 'Geeks') As 'Cmp_Value'
Output :
Example-4 : STRCMP() function to compare two string when at least one string is NULL.
Select STRCMP('Geek', NULL) As 'Cmp_Value'
Output :
Example-5 : STRCMP() function can also be used on column data. To demonstrate create a table named StudentDetails.
CREATE TABLE StudentDetails(
Student_id INT AUTO_INCREMENT,
First_name VARCHAR(100) NOT NULL,
Last_name VARCHAR(100) NOT NULL,
Student_Class VARCHAR(20) NOT NULL,
TotalExamGiven INT NOT NULL,
PRIMARY KEY(Student_id )
Inserting data into the Table :
INSERT INTO
StudentDetails(First_name, Last_name, Class, TotalExamGiven )
VALUES
('Sayan', 'Jana', 'IX', 8 ),
('Nitin', 'Sharma', 'X', 5 ),
('Aniket', 'Srivastava', 'XI', 6 ),
('Abdur', 'Ali', 'X', 7 ),
('Riya', 'Malakar', 'IX', 4 ),
('Jony', 'Patel', 'X', 10 ),
('Deepak', 'Saini', 'X', 7 ),
('Ankana', 'Biswas', 'XII', 5 ),
('Shreya', 'Majhi', 'X', 8 ) ;
To verify used the following command as follows.
SELECT * FROM StudentDetails;
Output :
STUDENT_ID |
FIRST_NAME |
LAST_NAME |
CLASS |
TOTALEXAMGIVEN |
1 |
Sayan |
Jana |
IX |
8 |
2 |
Nitin |
Sharma |
X |
5 |
3 |
Aniket |
Srivastava |
XI |
6 |
4 |
Abdur |
Ali |
X |
7 |
5 |
Riya |
Malakar |
IX |
4 |
6 |
Jony |
Patel |
X |
10 |
7 |
Deepak |
Saini |
X |
7 |
8 |
Ankana |
Biswas |
XII |
5 |
9 |
Shreya |
Majhi |
X |
8 |
Now, we are going to compare between First_Name and Last_Name column using STRCMP Function.
SELECT First_Name, Last_Name,
STRCMP(First_Name, Last_Name) AS Cmp_Value
FROM StudentDetails;
Output :
FIRST_NAME |
LAST_NAME |
CMP_VALUE |
Sayan |
Jana |
1 |
Nitin |
Sharma |
-1 |
Aniket |
Srivastava |
-1 |
Abdur |
Ali |
-1 |
Riya |
Malakar |
1 |
Jony |
Patel |
-1 |
Deepak |
Saini |
-1 |
Ankana |
Biswas |
-1 |
Shreya |
Majhi |
1 |
Similar Reads
TRIM() Function in MySQL
TRIM() function in MySQL is used to clean up data. It is also used to remove the unwanted leading and trailing characters in a string. Syntax : TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str) Parameter : This method accepts three-parameter as mentioned above and described below : BOTH | LEADIN
2 min read
RPAD() Function in MySQL
RPAD() function in MySQL is used to pad or add a string to the right side of the original string. Syntax : RPAD(str, len, padstr) Parameter : This function accepts three parameter as mentioned above and described below : str : The actual string which is to be padded. If the length of the original st
1 min read
PI() function in MySQL
PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally. Syntax : PI() Parameter : This method does not accept any parameter. Returns : It returns the Pi value i.e. 3.141593. Example-1 :
2 min read
SPACE() Function in MySQL
SPACE() function in MySQL is used to return a string consisting of specified empty space characters. Syntax : SPACE(num) Parameter : This method accepts one parameter as mentioned above and described below : num : It is an integer which indicates how many spaces are being contained. Returns : It ret
1 min read
POW() Function in MySQL
POW() function : This function in MySQL is used to return a results after raising a specified exponent number to a specified base number. For example if the base is 5 and exponent is 2, this will return a result of 25. Syntax : SELECT POW(x, y); Parameter : This method accepts two parameters as give
1 min read
TAN() Function in MySQL
TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
OCT() function in MySQL
OCT() function in MySQL is used to convert decimal number to octal. It returns equivalent octal value of a decimal number. Syntax : OCT(number) Parameter : This method accepts only one parameter. number : The decimal number which we want to convert. Returns : It returns octal value of a decimal numb
2 min read
ORD() Function in MySQL
ORD() function in MySQL is used to find the code of the leftmost character in a string . If the leftmost character is not a multibyte character, it returns ASCII value. And if the leftmost character of the string str is a multibyte character, ORD returns the code for that character, calculated from
3 min read
TRUNCATE() Function in MySQL
The TRUNCATE() function in MySQL is a valuable tool for manipulating numerical values by removing their decimal parts without rounding. It allows us to limit the precision of numbers to a specified number of decimal places or even truncate them to the nearest integer. In this article, We will learn
6 min read
SECOND() Function in MySQL
SECOND() function in MySQL is used to return the second portion of a specified time or date-time value. The first parameter in this function will be the date/Date Time. This function returns the seconds from the given date value. The return value (seconds) will be in the range of 0 to 59. In this fu
2 min read