informix
Informix Guide to GLS Functionality
SQL Features

Using Character Data Types

This section explains how a locale affects the way that a database server handles the following SQL character data types:

For the syntax of these data types, see the Informix Guide to SQL: Syntax. For descriptions of these data types, see the Informix Guide to SQL: Reference. For information about collation order, see Character Classes of the Code Set. For information about code-set conversion, see Performing Code-Set Conversion.

Locale-Specific Character Data

The choice of locale can affect the NCHAR and NVARCHAR character data types. This section describes how this choice affects these character data types.

The NCHAR Data Type

The NCHAR data type stores character data in a fixed-length field. This data can be a sequence of single-byte or multibyte letters, numbers, and symbols. However, the code set of your database locale must support this character data. NCHAR columns typically store names, addresses, phone numbers, and so on.

The syntax of the NCHAR data type is as follows.

Element Purpose
size Specifies the number of bytes in the column The total length of an NCHAR column cannot exceed 32,767 bytes. If you do not specify size, the default is NCHAR(1).

Because the length of this column is fixed, when the database server retrieves or sends an NCHAR value, it transfers exactly size bytes of data. If the length of a character string is shorter than size, the database server extends the string with spaces to make up the size bytes. If the string is longer than size bytes, the database server truncates the string.

Collating NCHAR Data

NCHAR is a locale-sensitive data type. The only difference between NCHAR and CHAR data types is the collation order. The database server collates data in NCHAR columns in localized order, if the locale defines a localized order. For most operations, the database server collates data in CHAR columns in code-set order.

Tip: The default locale (U.S. English) does not specify a localized order. Therefore, the database server sorts NCHAR data in code-set order. When you use the default locale, there is no difference between CHAR and NCHAR data. Handling NCHAR Data

Within a client application, always manipulate NCHAR data in the CLIENT_LOCALE of the client application. The client application performs code-set conversion of NCHAR data automatically if CLIENT_LOCALE differs from DB_LOCALE.

Multibyte Characters with NCHAR

To store multibyte character data in an NCHAR column, your database locale must support a code set with these same multibyte characters. When you store multibyte characters, make sure to calculate the number of bytes that are needed. The size parameter of the NCHAR data type refers to the number of bytes of storage that is reserved for the data.

Because one multibyte character uses several bytes for storage, the value of size bytes does not indicate the number of characters that the column can hold. The total number of multibyte characters that you can store in the column is less than the total number of bytes that you can store in the column. Make sure to declare the size value of the NCHAR column in such a way that it can hold enough characters for your purposes.

Treating NCHAR Values as Numeric Values

If you plan to perform calculations on numbers that are stored in a column, assign a numeric data type (such as INTEGER or FLOAT) to that column. The description of the CHAR data type in the Informix Guide to SQL: Reference provides detailed reasons why you should not store certain numberic values in CHAR values. The same reasons apply for certain numeric values as NCHAR values. Treat only numbers that have leading zeros (such as postal codes) as NCHAR data types. Use NCHAR only if you need to sort the numeric values in localized order.

Nonprintable Characters with NCHAR

An NCHAR value can include tabs, spaces, and nonprintable characters. Nonprintable NCHAR and CHAR values are entered, displayed, and treated similarly.

The NVARCHAR Data Type

The NVARCHAR data type stores character data in a variable-length field. This data can be a sequence of single-byte or multibyte letters, numbers, and symbols. However, the code set of your database locale must support this character data.

The syntax of the NVARCHAR data type is as follows:

Element Purpose
max Specifies the maximum number of bytes that can be stored in the column You must specify max of the NVARCHAR column. The size of this parameter cannot exceed 255 bytes. When you place an index on an NVARCHAR column, the maximum size is 254 bytes. You can store shorter, but not longer, character strings than the value that you specify.
reserve Specifies the minimum number of bytes that can be stored in the column This value can range from 0 to 255 bytes but must be less than the max size of the NVARCHAR column. If you do not specify a minimum space value, the default value of reserve is 0.

Specify the reserve parameter when you initially intend to insert rows with data values having few or no characters in this column but later expect the data to be updated with longer values.

Although use of NVARCHAR economizes on space that is used in a table, it has no effect on the size of an index. In an index that is based on an NVARCHAR column, each index key has a length equal to max bytes, the maximum size of the column.

The database server does not strip an NVARCHAR object of any user-entered trailing white space, nor does it pad the NVARCHAR object to the full length of the column. However, if you specify a minimum reserved space (reserve), and some of the data values are shorter than that amount, some of the space that is reserved for rows goes unused.

Collating NVARCHAR Data

The NVARCHAR data type is a locale-sensitive data type. The only difference between NVARCHAR and VARCHAR data types is the collation order. The database server collates data in NVARCHAR columns in localized order, if the locale defines a localized order. For most operations, the database server collates data in CHAR columns in code-set order.

Tip: The default locale (U.S. English) does not specify a localized order. Therefore, the database server sorts NVARCHAR data in code-set order. When you use the default locale, there is no difference between VARCHAR and NVARCHAR data. Handling NVARCHAR Data

Within a client application, always manipulate NVARCHAR data in the CLIENT_LOCALE of the client application. The client application performs code-set conversion of NVARCHAR data automatically if CLIENT_LOCALE differs from DB_LOCALE.

Multibyte Characters with NVARCHAR

To store multibyte character data in an NVARCHAR column, your database locale must support a code set with these same multibyte characters. When you store multibyte characters, make sure to calculate the number of bytes that are needed. The max parameter of the NVARCHAR data type refers to the maximum number of bytes that the column can store.

Because one multibyte character uses several bytes for storage, the value of max bytes does not indicate the number of characters that the column can hold. The total number of multibyte characters that you can store in the column is less than the total number of bytes that you can store in the column. Make sure to declare the max value of the NVARCHAR column so that it can hold enough characters for your purposes.

Nonprintable Characters with NVARCHAR

An NVARCHAR value can include tabs, spaces, and nonprintable characters. Nonprintable NVARCHAR characters are entered, displayed, and treated in the same way as nonprintable VARCHAR characters.

Tip: The database server interprets the null character (ASCII 0) as a C null terminator. Therefore, in NVARCHAR data, the null terminator acts as a string-terminator character. Storing Numeric Values in an NVARCHAR Column

When you insert a numeric value in a NVARCHAR column, the database server does not pad the value with trailing blanks up to the maximum length of the column. The number of digits in a numeric NVARCHAR value is the number of characters that you need to store that value. For example, the database server stores a value of 1 in the mytab table when it executes the following SQL statements:

Performance Considerations

The NCHAR data type is similar to the CHAR data type, and NVARCHAR is similar to the VARCHAR data type. The difference between these data types is as follows:

Localized collation depends on the sorting rules that the locale defines, not simply on the computer representation of the character (the code points). This difference means that the database server might perform complex processing to compare and collate NCHAR and NVARCHAR data. Therefore, access to NCHAR data might be slower with respect to comparison and collation than to access CHAR data. Similarly, access to data in an NVARCHAR column might be slower with respect to comparison and collation than access to the same data in a VARCHAR column.

Assess whether your character data needs to take advantage of localized order for collation and comparison. If code-set order is adequate, use the CHAR, LVARCHAR, and VARCHAR data types.

Other Character Data Types

The choice of locale can affect the following character data types:

This section describes how this choice affects each of these character data types.

The CHAR Data Type

The CHAR data type stores character data in a fixed-length field. This data can consist of letters, numbers, and symbols. The following list summarizes how choice of a locale affects the CHAR data type:

The VARCHAR Data Type

The VARCHAR data type stores character data of up to 255 bytes in a variable-length field. This data can consist of letters, numbers, and symbols. CHARACTER VARYING is handled exactly the same as VARCHAR. The following list summarizes how the choice of a locale affects the VARCHAR data type:

The LVARCHAR Data Type

The LVARCHAR data type stores character data greater than 255 bytes in a variable-length field. This data can consist of letters, numbers, and symbols. The database server also uses the LVARCHAR data type to represent the external format of an opaque data type.

LVARCHAR is similar to VARCHAR in the following ways:

The LVARCHAR data type supports SQL length functions similarly to the VARCHAR data type. For more information, see Using SQL Length Functions. For general information on the LVARCHAR data type, see the Informix Guide to SQL: Reference.

The TEXT Data Type

The TEXT data type stores any kind of text data. TEXT columns typically store memos, manual chapters, business documents, program source files, and other types of textual information. The following list summarizes how the choice of a locale affects the TEXT data type:


Informix Guide to GLS Functionality, Version 9.2
Copyright © 1999, Informix Software, Inc. All rights reserved