Javatpoint Logo
Javatpoint Logo

STRCMP Function in SQL

The STRCMP string function in SQL compares the two strings which are passed as the argument in the STRCMP function. This function returns 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 it returns 1 otherwise -1.

Syntax of STRCMP String Function

Syntax1: This syntax uses the STRCMP function with two columns of the SQL table:

In the syntax, we have to define the name of those columns whose length we want to compare.

Syntax3: This syntax uses the STRCMP function with two strings:

Examples of STRCMP String function

Example 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

Example 4: This example uses the STRCMP function with the table in Structured Query Language.

Now, we create the new SQL table, which helps to understand the STRCMP string function.

The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Worker_Info table:

The below INSERT queries insert the records of industry workers in the Worker_Info table:

The following SELECT statement displays the inserted records of the above Worker_Info table:


Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Address Worker_City Worker_Salary
1001 Arush Sharma 4001 Aman Vihar Delhi 20000
1002 Bulbul Roy 4002 Nirman Vihar Delhi 38000
1004 Saurabh Roy 4001 Sector 128 Mumbai 45000
1005 Shivani Singhania 4001 Vivek Vihar Kolkata 42000
1006 Avinash Sharma 4002 Sarvodya Calony Delhi 28000
1007 Shyam Besas 4003 Krishna Nagar Lucknow 35000

The following SELECT query uses the STRCMP function with the Worker_First_Name and Worker_City columns of the above Worker_Info table:

This SQL query compares the first name and city of each worker.

Output:

Worker_First_Name Worker_City Compare_Firstname_City
Arush Delhi 0
Bulbul Delhi 1
Saurabh Mumbai 1
Shivani Kolkata 0
Avinash Delhi 1
Shyam Lucknow -1

The following SELECT query uses STRCMP() with the Worker_first_Name and Worker_Last_Name columns of the above Worker_Info table:

This SQL statement compares the first name and last name of each Worker.

Output:

Worker_First_Name Worker_Last_Name STRCMP_Firstname_Lastname
Arush Sharma -1
Bulbul Roy 1
Saurabh Roy 1
Shivani Singhania -1
Avinash Sharma 1
Shyam Besas 0






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA