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
RTRIM() Function in MySQL RTRIM() : It is the function in MySQL that is used to remove trailing spaces from a string. Syntax : RTRIM(str) Parameter : RTRIM() function accepts one parameter as mentioned above and described below. str âThe string from which we want to remove trailing spaces. Returns : It returns a string after
3 min read
SUM() Function in MySQL The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read
SQRT() Function in MySQL The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications.In the article, we will cover
3 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
TIME() Function in MySQL The TIME() function in MySQL is used to extract the time portion from a date or datetime expression, returning the time in the format 'HH:MM'. This function is particularly useful when working with time components in databases, such as scheduling or logging systems. In this article, We will learn ab
4 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
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
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
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