Introduction to Oracle NVARCHAR2 Data Type

In Oracle database, NVARCHAR2 is a variable-length string data type that stores Unicode characters. This article introduces the syntax, use cases, examples, and conclusions of NVARCHAR2.

Syntax

The syntax of NVARCHAR2 data type is as follows:

NVARCHAR2(size [BYTE | CHAR])

Where size represents the maximum length of the data type, which can be any integer between 1 and 4000. BYTE and CHAR are optional parameters used to specify the length unit of the string. BYTE represents byte-length unit, and CHAR represents character-length unit. If not specified, the default is BYTE.

Use Cases

NVARCHAR2 is commonly used in scenarios that require storing Unicode character sets, such as storing text information in multiple languages. As NVARCHAR2 is variable-length, it provides more flexibility in storing strings of different lengths compared to fixed-length NCHAR type. Additionally, NVARCHAR2 can also be used for storing larger text information with a maximum length of 4000.

Examples

Here are two examples of NVARCHAR2 data type.

Example 1: Creating a table

Create a table with NVARCHAR2 type and insert a record.

CREATE TABLE my_table (
  id NUMBER,
  name NVARCHAR2(50)
);

INSERT INTO my_table (id, name)
VALUES (1, 'Hello, 世界');

Example 2: Querying the table

Query the previously created table and output the results.

SELECT * FROM my_table;

Result:

ID   NAME
---- ------------
1    Hello, 世界

Conclusion

NVARCHAR2 is a variable-length string data type that stores Unicode characters and is useful for storing text information in multiple languages or larger text information. Its maximum length can be specified as any integer between 1 and 4000, and the length unit can be chosen as BYTE or CHAR.