SQL Compare StringIn this SQL section, we will discuss how to compare two or more strings in the Structured Query Language We can compare two or more strings using the STRCMP string function, LIKE operator, and Equal operator. STRCMP String functionSTRCMP is a function of string that compares the specified two strings and gives 0 if the length of the first string is equal to the length of the second string. If the length of the first string is more than the length of the second string, then the function returns 1 otherwise -1. Syntax of STRCMP FunctionExamples of STRCMP String functionExample 1: The following SELECT query compares the length of JAVATPOINT and EXAMPLES strings: Output: 1 Example 2: The following SELECT query compares the length of two sentences passing in the STRCMP function: Output: -1 Example 3: The following SELECT query compares the length of two cities: Output: 0 LIKE OperatorThe LIKE operator matches the particular pattern with each row of the field and returns the matched values in the output. This operator is always used with the WHERE clause in the SQL statement. Syntax of Like operatorExample of LIKE OperatorLet's take the following Student table:
Query 1: The following query shows the record of those students from the above Student table whose First_Name starts with a 'B' letter: Output of above query:
As shown in the above output, the table only contains the record of Bhavesh and Bhavna students because their names begin with the B letter. Query 2: The following query shows the record of those students from the given Student table whose First_Name contains the character 'a' in any position: Output of above query:
Equal Operator (=)It is a type of comparison operator which shows the matched data from the given SQL table. This operator is highly used by the database users in Structured Query Language. This operator returns TRUE rows from the database table if the value of the column is the same as the value specified in the query. Syntax of Equal operator:Example of Equal OperatorLet's take the following Worker table:
The following query shows the record of those workers from the worker table whose Worker_Salary is 35000: Output:
Next TopicSQL Minus
|