VARCHAR2 Data Type

  1. VARCHAR2 is used to store variable-length character strings.
  2. When you define VARCHAR2(n), it can store up to n characters, but doesn’t pad extra spaces.
  3. If you insert fewer characters, no extra spaces are added.
  4. Maximum size of VARCHAR2 is 4000 bytes in SQL and up to 32767 bytes in PL/SQL.
  5. VARCHAR2 is best for data that varies in length, like names, emails, or addresses.
  6. Example: If you define VARCHAR2(10) and insert "ABC", Oracle stores it as "ABC" (no extra spaces).
  7. It saves storage space compared to CHAR for variable-length data.
  8. Comparisons consider the exact value (trailing spaces are not added).
  9. VARCHAR2 may have slightly higher processing overhead than CHAR for fixed-length data.
  10. It is defined by specifying the column name and maximum length, for example, a column of length 50.

 Example:

1.     VARCHAR2(5) → Input: A → Stored as: A

2.     VARCHAR2(5) → Input: AB → Stored as: AB

3.     VARCHAR2(5) → Input: #@! → Stored as: #@!

4.     VARCHAR2(4) → Input: (Hindi) → Stored as:

5.     VARCHAR2(6) → Input: 123 → Stored as: 123

6.     VARCHAR2(3) → Input: 😊 → Stored as: 😊

7.     VARCHAR2(8) → Input: مرحبا (Arabic) → Stored as: مرحبا

8.     VARCHAR2(10) → Input: HELLO → Stored as: HELLO

9.     VARCHAR2(4) → Input: π → Stored as: π

10.  VARCHAR2(7) → Input: テスト (Japanese) → Stored as: テスト

 

Key differences from CHAR:

·        VARCHAR2 does not add blank spaces.

·        Works for letters, numbers, symbols, emojis, and other languages.

·        Storage depends on actual length of the input.

If you want, I can also make a ready-to-use INSERT list for these 10 examples.

 

No comments:

Post a Comment