DB2 Tutorial

  • Home
  • Start Here
  • Basics
  • Views
  • Triggers
  • Indexes
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
Home » Db2 Basics » Db2 VARCHAR

Db2 VARCHAR

Summary: in this tutorial, you’ll learn how to use the Db2 VARCHAR data type to store variable-length character strings.

Overview of Db2 VARCHAR type

Db2 VARCHAR type is used to store variable-length character strings. To define a variable-length character string column, you use the following syntax:

column_name VARCHAR(n)

In this syntax, n is a positive integer that represents the maximum length of n bytes that the column can store. n also must be greater than zero and less than 32,740.

If you need to store a string whose length is longer than this, you should use the VARBINARY(n) data type instead.

Db2 VARCHAR type examples

Let’s create a new table to demonstrate the characteristics of VARCHAR(n) data type.

This statement creates a new table named db2_varchars that has a VARCHAR(20) column:

CREATE TABLE db2_varchars ( v VARCHAR(20) NOT NULL );

1) Insert a string into a variable-length character string column example

The following example inserts a string into the v column of the db2_varchars table:

INSERT INTO db2_varchars (v) VALUES ('Db2 Tutorial');

It worked as expected because the string Db2 Tutorial has the length which is less than 20.

However, this statement attempts to insert a string whose length is 22 into the VARCHAR(20) column and fails:

INSERT INTO db2_varchars(v) VALUES ('A Guide to Db2 VARCHAR');

Db2 issued the following error message:

SQL0433N Value "A Guide to Db2 VARCHAR" is too long.

2) Insert a Unicode string into a variable-length character string column example

This example inserts a Unicode string die Prüfung into the VARCHAR(20) column of the db2_varchars table:

INSERT INTO db2_varchars(v) VALUES ('die Prüfung');

To view the data in the db2_varchars table, you use the following SELECT statement:

SELECT * FROM db2_varchars;

Here is the output:

V -------------------- Db2 Tutorial die Prüfung

In this tutorial, you have learned how to use the Db2 VARCHAR data type to store variable-length character strings.

  • Was this tutorial helpful ?
  • YesNo
Previous Db2 CHAR
Next Db2 DATE Type

Getting Started

  • What is Db2
  • Installing Db2 Database Server
  • Db2 Sample Database
  • Creating a Db2 Sample Database
  • Connecting to a Db2 Database
  • Interacting with Db2 using SQL Developer

Data Manipulation

  • SELECT
  • ORDER BY
  • WHERE
  • SELECT DISTINCT
  • AND
  • OR
  • BETWEEN
  • LIKE
  • IN
  • LIMIT
  • FETCH
  • Join
  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • Self-Join
  • CROSS JOIN
  • GROUP BY
  • Subquery
  • HAVING
  • UNION
  • INTERSECT
  • EXCEPT
  • Common Table Expression or CTE
  • INSERT
  • INSERT Multiple Rows
  • INSERT INTO SELECT
  • UPDATE
  • DELETE

Managing Database Objects

  • CREATE TABLE
  • Identity Columns
  • ALTER COLUMN
  • ADD COLUMN
  • DROP COLUMN
  • DROP TABLE
  • TRUNCATE TABLE
  • RENAME TABLE

Db2 Constraints

  • Primary Key
  • Foreign Key
  • DEFAULT
  • NOT NULL
  • CHECK
  • UNIQUE

Db2 Data Types

  • Integer
  • Decimal
  • VARCHAR
  • CHAR
  • DATE
  • TIME
  • TIMESTAMP

Useful Functions & Expressions

  • CAST
  • CASE Expression
  • COALESCE

About db2tutorial.com

The db2tutorial.com website provides you with a comprehensive IBM DB2 tutorial with many practical examples and hands-on sessions.

Recent Tutorials

  • Db2 Functions
  • Db2 RANK
  • Db2 ROW_NUMBER
  • Db2 Window Functions
  • What is Db2

Site Links

  • Home
  • About Us
  • Contact Us
  • Privacy Policy
Copyright © © 2021 by www.db2tutorial.com. All Rights Reserved.