Oracle PL/SQL - CHAR and VARCHAR2 Blank-Padding Difference

Introduction

In the following code, both the CHAR variable and the VARCHAR2 variable have the maximum size of 10 characters.

Each variable receives a five-character value with one trailing blank.

The value assigned to the CHAR variable is blank-padded to 10 characters.

You cannot tell that one of the six trailing blanks in the resulting value was in the original value.

The value assigned to the VARCHAR2 variable is not changed, and you can see that it has one trailing blank.

Demo

SQL>
SQL> DECLARE-- from  ww  w.j a  v  a  2  s.  c  o  m
  2    first_name  CHAR(10 CHAR);
  3    last_name   VARCHAR2(10 CHAR);
  4  BEGIN
  5    first_name := '1234 ';
  6    last_name  := 'AAAA ';
  7
  8    DBMS_OUTPUT.PUT_LINE('*' || first_name || '*');
  9    DBMS_OUTPUT.PUT_LINE('*' || last_name || '*');
 10  END;
 11  /
*1234      *
*AAAA *

PL/SQL procedure successfully completed.

SQL>

Related Topic