VARCHAR(size)

The VARCHAR data type is a variable-length character string data type, with a maximum size.

Syntax

VARCHAR [ ( size [,reserve] ) ]
  1. size defines the maximum length of the character string. The upper limit is 65534.
  2. When size is not specified, the default length is 1.
  3. reserve is ignored; Its inclusion in the syntax is permitted for compatibility with the SQL data type.

Usage

VARCHAR variables are initialized to NULL in functions, modules and globals.

The size can be expressed in bytes or characters, depending on the length semantics used in programs.

VARCHAR variables store trailing blanks (i.e. "abc " is different from "abc"). Trailing blanks are displayed or printed in reports, but they are not significant in comparisons:
MAIN
  DEFINE vc VARCHAR(10)
  LET vc = "abc  "         -- two trailing blanks
  DISPLAY "[", vc ,"]"     -- displays [abc  ]
  IF vc == "abc" THEN    -- this is TRUE
     DISPLAY "equals"
  END IF
END MAIN

When you insert character data from VARCHAR variables into VARCHAR columns in a database table, the trailing blanks are kept. Likewise, when you fetch VARCHAR column values into VARCHAR variables, trailing blanks are kept.

MAIN
  DEFINE vc VARCHAR(10)
  DATABASE test1
  CREATE TABLE table1 ( k INT, x VARCHAR(10) )
  LET vc = "abc  "         -- two trailing blanks
  INSERT INTO table1 VALUES ( 1, vc )
  SELECT x INTO vc FROM table1 WHERE k = 1
  DISPLAY "[", vc ,"]"     -- displays [abc  ]
END MAIN

In SQL statements, the behavior of the comparison operators when using VARCHAR values differs from one database to the other. IBM® Informix® is ignoring trailing blanks, but most other databases take trailing blanks of VARCHAR values into account.