What is the major difference between Varchar2 and char

char, oracle, sql, varchar2

Solution

Simple example to show the difference:

SELECT 
    '"'||CAST('abc' AS VARCHAR2(10))||'"', 
    '"'||CAST('abc' AS CHAR(10))||'"' 
FROM dual;


'"'||CAST('ABC'ASVARCHAR2(10))||'"' '"'||CAST('ABC'ASCHAR(10))||'"'
----------------------------------- -------------------------------
"abc"                               "abc       "                   
1 row selected.

The CHAR is usefull for expressions where the length of charaters is always fix, e.g. postal code for US states, for example CA, NY, FL, TX

Problem

Creating Table: ``` CREATE TABLE test ( charcol CHAR(10), varcharcol VARCHAR2(10)); SELECT LENGTH(charcol), LENGTH(varcharcol) FROM test; ``` Result: ``` LENGTH(CHARCOL) LENGTH(VARCHARCOL) --------------- ------------------ 10 1 ``` Please Let me know what is the difference between Varchar2 and char? At what times we use both?

Original source