What is STRCMP() in SQL?

The STRCMP() function compares two strings sent as a parameter.

Figure 1 shows a visual representation of the STRCMP() function.

Figure 1: Visual representation of STRCMP() function

Syntax

STRCMP(str1, str2)

Parameter

The STRCMP() function takes two strings as a parameter.

Return value

The STRCMP() function compares two strings sent as a parameter.

  • If both the strings are equal, then this function returns 0.
  • If string-1 > string-2, then this function returns 1.
  • If string-1 < string-2, then this function returns -1.
  • If any of the strings is NULL, then this function returns NULL.

Code

-- Both the strings are same
SELECT STRCMP('educative','educative');
-- string-1 < string-2
SELECT STRCMP('edpresso','educative');
-- string-1 > string-2
SELECT STRCMP('educative','edpresso');
-- string-1 = NULL
SELECT STRCMP(NULL,'edpresso');

Free Resources