NVARCHAR2 in Oracle Example

In this Oracle tutorial, we will learn about the  NVARCHAR2 datatype in the Oracle database and demonstrate how to use it to create NVARCHAR2 columns for a table.

NVARCHAR2 in Oracle

NVARCHAR2 is a Unicode data type that can store Unicode characters. When the database was created, the national character set was specified as the NVARCHAR2 character set.

Variable-length character data is stored in the NVARCHAR2. The character length meanings are always the default and only length semantics for the NVARCHAR2 data type, hence they are always used when creating tables with NVARCHAR2 columns.

Syntax

We have understood the NVARCHAR2 datatype in a database and discussed the purpose of floating point. Next, we will take a step further and understand how to use it to create NVARCHAR2 columns for a table in Oracle.

For this, we need to understand the syntax for using an NVARCHAR2 datatype in Oracle 21c, which is given below.

NVARCHAR2(max_size BYTE)
NVARCHAR2(max_size CHAR)

Depending on the MAX STRING SIZE, the maximum length of any value that can be put in an NVARCHAR2 column is 32767 or 4000 bytes.

Also, check: Oracle Database vs MySQL Database

Example

After discussing the syntax, we will now discuss an example of how to use the NVARCHAR2 datatype in Oracle 21c. Here, we will discuss how to use the NVARCHAR2 datatype in the SQL Developer tool manually.

Recommendation: How to create a database in Oracle 19c

oracle nvarchar2

After creating the table, we will insert the column name and provide the datatype in this example, we utilize the NVARCHAR2 datatype with size(20).

what is nvarchar2 in oracle

Now we will insert the values into a table.

INSERT INTO student VALUES ('George');

Now we will use the select statement for showing the ‘student_name’ column value.

select * from student;
what is nvarchar2

Example:

Now we will create a table by statement and check how we can use the NVARCHAR2 datatype in the given employees table.

CREATE TABLE employees (
    emp_name NVARCHAR2(25)
);

The maximum allowed byte length for the emp name column is 200 bytes, and the default character set is UTF-16.

Note that the sum of the maximum character length and the maximum number of bytes in each character yields the maximum byte length.

Now we will insert the values in the emp_name column of the table

INSERT INTO employees VALUES('George');

To obtain more specific information on the value kept in the employees table, we utilize the DUMP() function.

SELECT
    emp_name,
    DUMP(emp_name,267)
FROM
    employees;
Nvarchar2 datatype in oracle database

The result shows that the datatype is and the length is 12 bytes (6 characters, 2 bytes each).

Read: How to Check Oracle Database Version

Oracle NVARCHAR2 datatype max length

Depending on the MAX STRING SIZE, the maximum length of any value that can be put in an NVARCHAR2 column is 32767 or 4000 bytes.

Conclusion

In this Oracle tutorial, we have understood how to define and use the Oracle NVARCHAR2 Datatype and covered a few sample examples related to it.

Also, take a look at some more Oracle tutorials.

Top 200 SQL Server Interview Questions and Answers

Free PDF On Top 200 SQL Server Interview Questions And Answers

Download A 40 pages PDF And Learn Now.