rules repository

G-2310

🆓
Error

Avoid using CHAR data type.

Reason

char is a fixed length data type, which should only be used when appropriate. char columns/variables are always filled to its specified lengths; this may lead to unwanted side effects and undesired results.

Example

Non-Compliant Example

create or replace package types_up
is
   subtype description_type is char(200);
end types_up;
/
Issues
LineColumnMessage
332Use VARCHAR2 instead of CHAR data type.

Explanation

Unexpected trailing spaces can lead to wrong results.

with
   dept as (
      select cast(department_name as varchar2(30 char)) as dname_vc2
            ,cast(department_name as char(30 char)) as dname_char
        from departments
   )
select count(*)
  from dept
 where dname_vc2 = dname_char;
  COUNT(*)
----------
         0

Compliant Solution - ★★★★★

create or replace package types_up
is
   subtype description_type is varchar2(200 char);
end types_up;
/

References