I have the following Oracle PL/SQL codes that may be rusty from you guys perspective:
DECLARE
str1 varchar2(4000);
str2 varchar2(4000);
BEGIN
str1:='';
str2:='sdd';
IF(str1<>str2) THEN
dbms_output.put_line('The two strings is not equal');
END IF;
END;
/
This is very obvious that two strings str1 and str2 are not equal, but why ‘The two strings are not equal’ was not printed out? Do Oracle have another common method to compare two string?
As Phil noted, the empty string is treated as a NULL, and NULL is not equal or unequal to anything. If you expect empty strings or NULLs, you’ll need to handle those with
NVL():Concerning null comparisons:
According to the Oracle 12c documentation on NULLS, null comparisons using
IS NULLorIS NOT NULLdo evaluate toTRUEorFALSE. However, all other comparisons evaluate toUNKNOWN, notFALSE. The documentation further states:A reference table is provided by Oracle:
I also learned that we should not write PL/SQL assuming empty strings will always evaluate as NULL: