Table of Contents

Oracle Database - Bytes or Characters for VARCHAR2 and CHAR

About

Text data is encoded/stored/transformed on the computer in bytes thanks to a character set that maps text to bytes.

Historically, the character sets were single-byte character sets that could hold 256 characters. It meant that one character needed only one byte. But with the globalization of the world, it was not enough anymore to hold all character's languages, and the multi-byte character sets were born.

The problems and Oracle error

The issues are that :

Leadings to these Oracle errors :

How to specify the length unit (byte or char)?

With SQL

The VARCHAR2 and CHAR SQL data types support two methods of specifying lengths:

VARCHAR2(10 byte)
VARCHAR2(10 char)

With the NLS_LENGTH_SEMANTICS setting

The session or system parameter NLS_LENGTH_SEMANTICS changes the default text length unit from BYTE to CHAR.

alter session set nls_length_semantics=char;
alter session set nls_length_semantics=byte;
alter system set nls_length_semantics=char scope=both;
alter system set nls_length_semantics=byte scope=both;

With the Text functions

You can specify the length unit in the textual functions by choosing:

For instance,

SUBSTR('abcdefg',3,4) 
SUBSTRB('abcdefg',3,4) 

1) 2)