The VARCHAR
datatype is synonymous with the VARCHAR2
datatype. To avoid possible changes in behavior, always use the VARCHAR2
datatype to store variable-length character strings.
If your database runs on a single-byte character set (e.g. US7ASCII
, WE8MSWIN1252
or WE8ISO8859P1
) it does not make any difference whether you use VARCHAR2(x BYTE)
or VARCHAR2(x CHAR)
.
It makes only a difference when your DB runs on multi-byte character set (e.g. AL32UTF8
or AL16UTF16
). You can simply see it in this example:
CREATE TABLE my_table (
VARCHAR2_byte VARCHAR2(1 BYTE),
VARCHAR2_char VARCHAR2(1 CHAR)
);
INSERT INTO my_table (VARCHAR2_char) VALUES ('€');
1 row created.
INSERT INTO my_table (VARCHAR2_char) VALUES ('ü');
1 row created.
INSERT INTO my_table (VARCHAR2_byte) VALUES ('€');
INSERT INTO my_table (VARCHAR2_byte) VALUES ('€')
Error at line 10
ORA-12899: value too large for column "MY_TABLE"."VARCHAR2_BYTE" (actual: 3, maximum: 1)
INSERT INTO my_table (VARCHAR2_byte) VALUES ('ü')
Error at line 11
ORA-12899: value too large for column "MY_TABLE"."VARCHAR2_BYTE" (actual: 2, maximum: 1)
VARCHAR2(1 CHAR)
means you can store up to 1 character, no matter how many byte it has. In case of Unicode one character may occupy up to 4 bytes.
VARCHAR2(1 BYTE)
means you can store a character which occupies max. 1 byte.
If you don't specify either BYTE
or CHAR
then the default is taken from NLS_LENGTH_SEMANTICS
session parameter.
Unless you have Oracle 12c where you can set MAX_STRING_SIZE=EXTENDED
the limit is VARCHAR2(4000 CHAR)
However, VARCHAR2(4000 CHAR)
does not mean you are guaranteed to store up to 4000 characters. The limit is still 4000 bytes, so in worst case you may store only up to 1000 characters in such field.
See this example (€
in UTF-8 occupies 3 bytes):
CREATE TABLE my_table2(VARCHAR2_char VARCHAR2(4000 CHAR));
BEGIN
INSERT INTO my_table2 VALUES ('€€€€€€€€€€');
FOR i IN 1..7 LOOP
UPDATE my_table2 SET VARCHAR2_char = VARCHAR2_char ||VARCHAR2_char;
END LOOP;
END;
/
SELECT LENGTHB(VARCHAR2_char) , LENGTHC(VARCHAR2_char) FROM my_table2;
LENGTHB(VARCHAR2_CHAR) LENGTHC(VARCHAR2_CHAR)
---------------------- ----------------------
3840 1280
1 row selected.
UPDATE my_table2 SET VARCHAR2_char = VARCHAR2_char ||VARCHAR2_char;
UPDATE my_table2 SET VARCHAR2_char = VARCHAR2_char ||VARCHAR2_char
Error at line 1
ORA-01489: result of string concatenation is too long
See also Examples and limits of BYTE and CHAR semantics usage (NLS_LENGTH_SEMANTICS) (Doc ID 144808.1)