1

Here's a question about string comparison in Oracle PL/SQL. I'm testing a few simple snippets involving blank-padding and non-blank-padding comparison and I run into a specific case where I cannot get what the PL/SQL engine is doing.

Here's my code (I'm running this script on Oracle 12c)

set serveroutput on;
declare
   l_char1      char(35)     := 'Hello';
   l_char2      char(30)     := 'Hello  ';
   l_varchar21  varchar2(35) := 'Hello';
   l_varchar22  varchar2(35) := 'Hello  ';
begin
   /* When a comparison is made between variables of type CHAR, 
   a blank-padding comparison takes place. */
   if ( l_char1 = l_char2) then
      dbms_output.put_line('First comparison is TRUE');
   else
      dbms_output.put_line('First comparison is FALSE');
   end if;

   /* When at least one of the involved variables is a VARCHAR2, 
   then a non-blank-padding comparison takes place. None of the     
   variables involved in the comparison is then modified. */
   if ( l_char1 = l_varchar21) then
      dbms_output.put_line('Second comparison is TRUE');
   else
      dbms_output.put_line('Second comparison is FALSE');
   end if;

   /* yet another non-blank-padding comparison */
   if ( l_varchar21 = l_varchar22) then
      dbms_output.put_line('Third comparison is TRUE');
   else
      dbms_output.put_line('Third comparison is FALSE');
   end if;

   /* Strange behaviour: I supposed both string literals to be
   treated as VARCHAR2, so I expected to get FALSE. */
   if ( 'MP' = 'MP ') then
      dbms_output.put_line('Fourth comparison is TRUE');
   else
      dbms_output.put_line('Fourth comparison is FALSE');
   end if;
end;

Here's the output

Procedura PL/SQL completata correttamente.
First comparison is TRUE
Second comparison is FALSE
Third comparison is FALSE
Fourth comparison is TRUE    ** I expected FALSE **

About the string literals comparison, I expected to get what is reported on Oracle documentation; I quote the key sentence:

Trailing blanks are significant within string literals, so 'abc' and 'abc ' are different. Trailing blanks in a string literal are not trimmed during PL/SQL processing, although they are trimmed if you insert that value into a table column of type CHAR. For additional information, including NCHAR string literals, see "String Literals".

Any idea to explain what I found? Could this depend on a specific setting in my Oracle environment/session ?

1 Answer 1

2

this is because 'MP' and 'MP ' are CHAR values and oracle automatically pads whatever value you assign to that variable with spaces to the maximum length specified

If you declare a CHAR variable with a length greater than 1, Oracle Database automatically pads whatever value you assign to that variable with spaces to the maximum length

see the OraMag article with similar to your example http://www.oracle.com/technetwork/issue-archive/2011/11-sep/o51plsql-453456.html

BEGIN
  IF 'Logic' = 'Logic     '
  THEN
    DBMS_OUTPUT.put_line ('Equal');
  ELSE
    DBMS_OUTPUT.put_line ('Not Equal');
  END IF;
END;

show Equal because "Logic" was padded with spaces to "Logic "

4
  • I came to the same conclusion, but what about the quote from Oracle documentation? Did I just misunderstood what they mean or is that an error to be fixed ?
    – Max
    Dec 13, 2015 at 16:15
  • docs.oracle.com/cd/B14117_01/server.101/b10759/… Blank-Padded Comparison Semantics If the two values have different lengths, then Oracle first adds blanks to the end of the shorter one so their lengths are equal. .... Oracle uses blank-padded comparison semantics only when both values in the comparison are either expressions of datatype CHAR, NCHAR, text literals, ...
    – are
    Dec 13, 2015 at 16:20
  • @are - is begin - end block mandatory when you write queries like this? In sql server you don't need this, but in oracle I see that things are not working without.
    – FrenkyB
    Apr 18, 2020 at 15:52
  • 1
    @FrenkyB this is anonymous block. the structure is common in many database languages. as I remember you skip begin...end in MSSQL script but if you need for example catch exceptions you will need it same way as in Oracle
    – are
    Apr 19, 2020 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.