Varchar2 和 char 之间的主要区别是什么

2021-12-01 00:00:00 sql char oracle varchar2

创建表格:

CREATE TABLE test (
charcol    CHAR(10),
varcharcol VARCHAR2(10));

SELECT LENGTH(charcol), LENGTH(varcharcol) FROM test;

结果:

LENGTH(CHARCOL) LENGTH(VARCHARCOL)
--------------- ------------------
             10                  1 

请让我知道 Varchar2 和 char 有什么区别?我们什么时候使用两者?

Please Let me know what is the difference between Varchar2 and char? At what times we use both?

推荐答案

简单的例子来说明区别:

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.

CHAR 对于字符长度总是固定的表达式很有用,例如美国各州的邮政编码,例如 CA、NY、FL、TX

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

相关文章