The CHAR Datatype

The CHAR datatype specifies a fixed-length character string. When you declare a fixed-length string, you also specify a maximumlength for the string, which can range from 1 to 32767 bytes. (Again, this is much higher than that for the CHAR datatype in the Oracle RDBMS, which is only 2000, or 255 prior to Oracle8i. You can specify the length in terms of bytes or in terms of characters. For example, the following two declarations create strings of 100 bytes and 100 characters respectively:

feature_name CHAR(100 BYTE);feature_name CHAR(100 CHAR);

The actual number of bytes in a 100-character string depends on the underlying database character set. If you are using a variable-width character set, PL/SQL will allocate enough bytes to the string to accommodate the specified number of worst-case characters. For example, UTF-8 uses between one and three bytes per character, so PL/SQL will assume the worst and allocate three bytes x 100 characters, for a total of 300 bytes.

Is Fixed-Length Really Fixed? You can get very unusual results when you declare CHAR variables using byte semantics and use those variables to store characters in a multibyte character set. The following example was generated on a system using UTF-8 as the database character set: DECLARE x CHAR(3);BEGIN --Assign a single-byte character x := 'a'; DBMS_OUTPUT.PUT_LINE(LENGTH(x)); --and now a two-byte character x := 'ã'; DBMS_OUTPUT.PUT_LINE(LENGTH(x)); --and now two characters for a total --of three bytes x := 'ãa'; DBMS_OUTPUT.PUT_LINE(LENGTH(x));END; The output is: 322 Isn't it interesting that the length of a supposedly fixed-length string changes? Isn't it even more interesting that the length of ã is exactly the same as the length of ãa? What's happening here is that the LENGTH function is counting characters. In the first case, the string has the letter "a" followed by two spaces, for a total of three characters. In the next case, it's the two-byte character ã followed by only one space, for a total of two characters in three bytes. In the last case, it's the two-byte character ã followed by the single-byte letter "a", also for a total of two characters. Run this same piece of code with x declared as CHAR(3 CHAR), and the length will consistently be three characters. Thus, when working with multibyte characters, it might be best to always use character semantics in your declarations.

 

If you leave off the BYTE or CHAR qualifier, the results will depend on the setting of the NLS_LENGTH_SEMANTICS parameter. Assuming the default setting, the following declaration results in a 100-byte string:

feature_name CHAR(100);

If you do not specify a length for the string, PL/SQL declares a string of one byte. Suppose you declare a variable as follows:

feature_name CHAR;

As soon as you assign a string of more than one character to feature_name, PL/ SQL will raise the generic VALUE_ERROR exception. It will not tell you where it encountered this problem. So if you do get this error, check your variable declarations for a lazy use of CHAR. To avoid mistakes and to prevent future programmers from wondering about your intent, you should always specify a length when you use the CHAR datatype. Several examples follow:

yes_or_no CHAR (1) DEFAULT 'Y';line_of_text CHAR (80 CHAR); --Always a full 80 characters!whole_paragraph CHAR (10000 BYTE); --Think of all the spaces...

Because CHAR is fixed-length, PL/SQL will right-pad any value assigned to a CHAR variable with spaces to the maximum length specified in the declaration.

Prior to Oracle7, the CHAR datatype was variable-length; Oracle did not support a fixed-length character string datatype and prided itself on that fact. To improve compatibility with IBM relational databases and to comply with ANSI standards, Oracle7 reintroduced CHAR as a fixed-length datatype and offered VARCHAR2 as the variable-length datatype.