Home > Software engineering >  What is the reason of using 'CHAR' with DATATYPE SIZE when declaring variable
What is the reason of using 'CHAR' with DATATYPE SIZE when declaring variable

Time:01-17

WHY THE 'CHAR' IS USING WITH SIZE ?

declare
  lf  constant varchar2(1 char) := chr(10);
  lf2 constant varchar2(2 char) := lf || lf;
begin
dbms_output.put_line('LINE-X'||lf||'LINE-Y'||lf2||'LINE-Z');
end;

RESULT IS FINE AS BELOW

LINE-X LINE-Y

LINE-Z

PL/SQL procedure successfully completed.

AND IF IT IS POSIBLE WHICH SINARIO WE CAN USE 'CHAR'?

CodePudding user response:

When declaring a varchar2 datatype variable (or column), you have two options when setting its size: char and byte:

SQL> create table test
  2    (a    varchar2(1 byte),
  3     b    varchar2(2 char)
  4    );

Table created.

SQL>

The first option (byte) means that you reserved exactly one byte for that column.

The second option (char) means that you chose to store one character into that column. In many/most cases, there's no difference. But, if you use character set that contains multibyte characters, you can't tell how many bytes will each character occupy so - for example - varchar2(2 byte) doesn't guarantee that you'll manage to store all characters (for example the one that uses 3 bytes won't fit).

  • Related