Language basics / Data types |
The VARCHAR data type is a variable-length character string data type, with a maximum size.
VARCHAR [ ( size [,reserve] ) ]
The VARCHAR type is typically used to store variable-length character strings such as names, addresses and comments.
The size can be expressed in bytes or characters, depending on the length semantics used in programs. For more details about character length semantics, see Length semantics settings.
When size is not specified, the default length is 1.
VARCHAR variables are initialized to NULL in functions, modules and globals.
MAIN DEFINE c VARCHAR(10) LET c = "abcdef" END MAIN
MAIN DEFINE vc VARCHAR(10) LET vc = "abc " -- a b c + 2 white spaces DISPLAY "[", vc ,"]" -- displays [abc ] END MAIN
MAIN DEFINE vc VARCHAR(10) LET vc = "abc " -- a b c + 2 white spaces IF vc == "abc " THEN -- evaluates to TRUE DISPLAY "equals" END IF END MAIN
MAIN DEFINE vc VARCHAR(50), da DATE, dec DECIMAL(10,2) LET da = TODAY LET dec = 345.12 LET vc = da, " : ", dec 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. For more details, see SQL portability.