Char Datatype in Oracle Database

In this Oracle tutorial, we will learn how to use the Char datatype in oracle database 21c. Moreover, we will take a look at multiple examples and we will check with the use of char datatype.

Introduction to Oracle Char datatype

The character values are stored using the char data type. It is a fixed-length data type, meaning that once created, the size cannot be changed during execution.

A character string with a size of 1 to 2000 bytes can be stored in a CHAR data type. Both alphanumeric and regular characters can be stored there.

Oracle Char datatype Syntax

Until now, we have understood the Char datatype in a database and discussed the purpose of Characters. Next, we will take a step further and understand how to use it to create Char columns for a table.

For this, we need to understand the syntax to use a Char datatype in oracle 21c which is given below.

CHAR(length BYTE)
CHAR(length CHAR)

We must specify a string length, either in bytes or characters, to define a CHAR column. Oracle utilizes the BYTE by default if you don’t clearly mention CHAR or BYTE after the length.

Note:

If you save Six characters in char(11), Oracle will store Six bytes and pad the remaining five bytes to the right side

Also, check: Float Datatype in Oracle Database

Oracle Char datatype Examples

After discussing the syntax, we will now discuss the example of how to use the Char datatype in oracle 21c.

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

CREATE TABLE customers (customer_name CHAR(11));

First, we created a table named customers with a char column (customer_name). The length of the column is 11 bytes.

We will insert the characters and display the character values in the customer_name column.

INSERT INTO customers VALUES ('John');

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

select * from customers;
character datatype in oracle
character datatype in oracle

Example:

Here we will discuss how to use the Char datatype in the SQL developer tool manually.

student name table created in oracle
student name table created in oracle

As you can see in the screenshot this is the table for our example execution and this student table is located in the pdb database.

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

Added column in the table
Added column in the table

Now we will insert the values into a table.

INSERT INTO student VALUES ('Micheal');
char datatype in oracle database
char datatype in the oracle database

Read: Number Datatype in Oracle Database

Oracle char datatype max length

A character string with a maximum length of 2000 bytes can be stored using the char data type. Additionally, the memory stores one byte for every character. Memory will be wasted because the size is set and cannot be modified.

Conclusion

So, in this Oracle tutorial, we understood how to define and use the Oracle Char Datatype. And we have also covered a few sample examples related to it.

You may also like to read the following Oracle tutorials