The data types supported by the language:
Data Type | Description |
String Data Types | |
CHAR | Fixed size character strings |
VARCHAR | Variable size character strings |
STRING | Dynamic size character strings |
Date and Datetime Data Types | |
DATE | Simple calendar dates |
DATETIME | High precision date and hour data |
INTERVAL | High precision time intervals |
Numeric Data Types | |
BIGINT | 8 byte signed integer |
INTEGER | 4 byte signed integer |
SMALLINT | 2 byte signed integer |
TINYINT | 1 byte signed integer |
FLOAT | 8 byte floating point decimal |
4 byte floating point decimal | |
DECIMAL | High precision decimals |
MONEY | High precision decimals with currency formatting |
Large Data Types | |
BYTE | Large binary data (images) |
TEXT | Large text data (plain text) | BOOLEAN Data Types |
BOOLEAN | TRUE/FALSE bool |
See also: Data Conversions, Variables, Programs.
The CHAR data type is a fixed-length character string data type.
CH
AR[ACTER] [ (size) ]
CHAR variables are initialized to NULL in functions, modules and globals.
The size defines the number of bytes the variable can store. It is important to distinguish bytes from characters, because in a multi-byte character set, one character may be encoded on several bytes. For example, in the ISO-8859-1 character set, "forêt" uses 5 bytes, while in the UTF-8 multi-byte character set, the same word occupies 6 bytes, because the "ê" letter is coded with two bytes.
CHAR variables are always filled with trailing blanks, but the trailing blanks are not significant in comparisons:
01
MAIN02
DEFINE c CHAR(10)03
LET c = "abcdef"04
DISPLAY "[", c ,"]" -- displays [abcdef ]05
IF c == "abcdef" THEN -- this is TRUE06
DISPLAY "equals"07
END IF08
END MAIN
The VARCHAR data type is a variable-length character string data type, with a maximum size.
VARC
HAR [ ( size, [
reserve ] ) ]
VARCHAR variables are initialized to NULL in functions, modules and globals.
The size defines the maximum number of bytes the variable can store. It is important to distinguish bytes from characters, because in a multi-byte character set, one character may be encoded on several bytes. For example, in the ISO-8859-1 character set, "forêt" uses 5 bytes, while in the UTF-8 multi-byte character set, the same word occupies 6 bytes, because the "ê" letter is coded with two bytes.
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:
01
MAIN02
DEFINE vc VARCHAR(10)03
LET vc = "abc " -- two trailing blanks04
DISPLAY "[", vc ,"]" -- displays [abc ]05
IF vc == "abc" THEN -- this is TRUE06
DISPLAY "equals"07
END IF08
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.
Note that 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. See SQL Programming for more details.01
MAIN02
DEFINE vc VARCHAR(10)03
DATABASE test104
CREATE TABLE table1 ( k INT, x VARCHAR(10) )05
LET vc = "abc " -- two trailing blanks06
INSERT INTO table1 VALUES ( 1, vc )07
SELECT x INTO vc FROM table1 WHERE k = 108
DISPLAY "[", vc ,"]" -- displays [abc ]09
END MAIN
The STRING data type is a variable-length, dynamically allocated character string data type, without limitation.
STRING
The behavior of a STRING variable is similar to the
VARCHAR data
type. For example, as VARCHAR variables, STRING variables have significant trailing
blanks (i.e. "abc
"
is different from "abc"
). There is no size limitation, it depends on available memory.
Unlike VARCHAR, a STRING can hold a value of zero length without being NULL. For example, if you trim a string variable with the trim() method and if the original value is a set of blank characters, the result is an empty string. But testing the variable with the IS NULL operator will evaluate to FALSE. Using a VARCHAR with the CLIPPED operator would give a NULL string in this case.
STRING variables are initialized to NULL in functions, modules and globals.
The STRING data type is typically used to implement utility functions manipulating character string with unknown size. It cannot be used to store SQL character string data, because databases have rules that need a maximum size as for CHAR and VARCHAR types.
The STRING data type cannot be used as SQL parameter of fetch buffer.Variables declared with the STRING data type can use built-in class methods such as getLength() or toUpperCase().
Note that the STRING methods are all based on byte-length-semantics. In a multi-byte environment, the getLength() method returns the number of bytes, which can be different from the number of characters.
Object Methods | |
Name | Description |
append( str STRING ) RETURNING STRING |
Returns a new string made by adding str to the end of the current string. |
equals( src STRING ) RETURNING INTEGER |
Returns TRUE if the string passed as parameters matches the current string. If one of the strings is NULL the method returns FALSE. |
equalsIgnoreCase( src STRING ) RETURNING INTEGER |
Returns TRUE if the string passed as parameters matches the current string, ignoring character case. If one of the strings is NULL the method returns FALSE. |
getCharAt( pos INTEGER ) RETURNING STRING |
Returns the character at the byte position pos (starts at 1). Returns NULL if the position does not match a valid character-byte position in the current string or if the current string is null. |
getIndexOf( str STRING, spos INTEGER
) RETURNING INTEGER |
Returns the position of the sub-string str in the current string, starting from byte position spos. Returns zero if the sub-string was not found. Returns -1 if string is NULL. |
getLength( ) RETURNING INTEGER |
This method counts the number of bytes, including trailing blanks. The LENGTH() built-in function ignores trailing blanks. |
subString( spos INTEGER, epos INTEGER ) RETURNING STRING |
Returns the sub-string starting at byte position spos and ending at epos. Returns NULL if the positions do not delimit a valid sub-string in the current string, or if the current string is null. |
toLowerCase( ) RETURNING STRING |
Converts the current string to lowercase. Returns NULL if the string is null. |
toUpperCase( ) RETURNING STRING |
Converts the current string to uppercase. Returns NULL if the string is null. |
trim( ) RETURNING STRING |
Removes white space characters from the beginning and end of the current string. Returns NULL if the string is null. |
trimLeft( ) RETURNING STRING |
Removes white space characters from the beginning of the current string. Returns NULL if the string is null. |
trimRight( ) RETURNING STRING |
Removes white space characters from the end of the current string. Returns NULL if the string is null. |
01
MAIN02
DEFINE s STRING03
LET s = "abcdef "04
DISPLAY s || ". (" || s.getLength() || ")"05
IF s.trimRight() = "abcdef" THEN06
DISPLAY s.toUpperCase()07
END IF08
END MAIN
The BIGINT data type is used for storing very large whole numbers.
BIGINT
The storage of BIGINT variables is based on 8 bytes of signed data ( = 64 bits ).
The value range is from -9,223,372,036,854,775,807 to +9,223,372,036,854,775,807.
When assigning a whole number that exceeds the BIGINT range, the overflow error -1284 will be raised.
BIGINT variables are initialized to zero in functions, modules and globals.
The BIGINT type can be used to define variables used in SQL
statements. It can be used to store data from INT8, SERIAL8, BIGINT and BIGSERIAL
columns. Note that SQLCA.SQLERRD[2]
is an INTEGER, not a BIGINT: To retrieve the last generated BIGSERIAL or SERIAL8, you must
use the IBM Informix dbinfo() SQL function (SELECT dbinfo('bigserial') FROM
systables WHERE tabid = 1
).
01
MAIN02
DEFINE i BIGINT03
LET i = 922337203685477560004
DISPLAY i05
END MAIN
The INTEGER data type is used for storing large whole numbers.
INT[EGER]
The storage of INTEGER variables is based on 4 bytes of signed data ( = 32 bits ).
The value range is from -2,147,483,647 to +2,147,483,647.
When assigning a whole number that exceeds the INTEGER range, the overflow error -1215 will be raised.
INTEGER variables are initialized to zero in functions, modules and globals.
The INTEGER type can be used to define variables storing values from SERIAL columns.
01
MAIN02
DEFINE i INTEGER03
LET i = 123456704
DISPLAY i05
END MAIN
The SMALLINT data type is used for storing small whole numbers.
SMALLINT
The storage of SMALLINT variables is based on 2 bytes of signed data ( = 16 bits ).
The value range is from -32,767 to +32,767.
When assigning a whole number that exceeds the SMALLINT range, the overflow error -1214 will be raised.
SMALLINT variables are initialized to zero in functions, modules and globals.
01
MAIN02
DEFINE i SMALLINT03
LET i = 123404
DISPLAY i05
END MAIN
The TINYINT data type is used for storing very small whole numbers.
TINYINT
The storage of TINYINT variables is based on 1 byte of signed data ( = 8 bits ).
The value range is from -128 to +127.
When assigning a whole number that exceeds the TINYINT range, the overflow error -8097 will be raised.
TINYINT variables are initialized to zero in functions, modules and globals.
Note that the TINYINT variables cannot be NULL.01
MAIN02
DEFINE i TINYINT03
LET i = 10104
DISPLAY i05
END MAIN
The FLOAT data type stores values as double-precision floating-point binary numbers with up to 16 significant digits.
{ FLOAT | DOUBLE PRECISION } [(precision)]
The storage of FLOAT variables is based on 8 bytes of signed data ( = 64 bits ), this type is equivalent to the double data type in C.
FLOAT variables are initialized to zero in functions, modules and globals.
FLOAT values can be converted to strings according to the DBMONEY environment variable (defines the decimal separator).
Note that this data type it is not recommended for exact decimal storage; use the DECIMAL data type instead.The SMALLFLOAT data type stores values as single-precision floating-point binary numbers with up to 8 significant digits.
{ SMALLFLOAT | REAL }
The storage of SMALLFLOAT variables is based on 4 bytes of signed data ( = 32 bits ), this type is equivalent to the float data type in C.
SMALLFLOAT variables are initialized to zero in functions, modules and globals.
SMALLFLOAT values can be converted to strings according to the DBMONEY environment variable (defines the decimal separator).
Note that this data type it is not recommended for exact decimal storage; use the DECIMAL data type instead.The DECIMAL data type is provided to handle large numeric values with exact decimal storage.
{ DEC[IMAL] | NUMERIC }
[ ( precision[,scale]
)
]
The DECIMAL data type must be used for storing number with fractional parts that must be calculated exactly.
DECIMAL variables are initialized to NULL in functions, modules and globals.
The largest absolute value that a DECIMAL(p,s) can store without errors is 10p-s - 10s. The stored value can have up to 30 significant decimal digits in its fractional part, or up to 32 digits to the left of the decimal point.
When you specify both the precision and scale, you define a decimal with a fixed point arithmetic. If the data type declaration specifies a precision but no scale, it defines a floating-point number with precision significant digits. If the data type declaration specifies no precision and scale, the default is DECIMAL(16), a floating-point number with a precision of 16 digits.
DECIMAL values can be converted to strings according to the DBMONEY environment variable (defines the decimal separator).
In ANSI-compliant databases, DECIMAL data types do not provide floating point numbers. When you define a database column as DECIMAL(16), it is equivalent to a DECIMAL(16,0) declaration. You should always specify the scale to avoid mistakes.When the default exception handler is used, if you try to assign a value larger than the Decimal definition (for example, 12345.45 into DECIMAL(4,2) ), no out of range error occurs, and the variable is assigned with NULL. If WHENEVER ANY ERROR is used, it raises error -1226. If you do not use WHENEVER ANY ERROR, the STATUS variable is not set to -1226.
A couple of precision math functions are available, to be used with DECIMAL values. These functions have a higher precision as the standard C library functions based on C double data type, which is equivalent to FLOAT:
01
MAIN02
DEFINE d1 DECIMAL(10,4)03
DEFINE d2 DECIMAL(10,3)04
LET d1 = 1234.456705
LET d2 = d1 / 3 -- Rounds decimals to 3 digits06
DISPLAY d1, d207
END MAIN
The MONEY data type is provided to store currency amounts with exact decimal storage.
MONEY [ (precision[,scale])
]
The MONEY data type is provided to store currency amounts. Its behavior is similar to the DECIMAL data type, with some important differences:
A MONEY variable is displayed with the currency symbol defined in the DBMONEY environment variable.
You cannot define floating-point numbers with MONEY: If you do not specific the scale in the data type declaration, it defaults to 2. If no precision / scale parameters are specified, MONEY is interpreted as a DECIMAL(16,2).
See also the DECIMAL data type.The DATE data type stores calendar dates with a Year/Month/Day representation.
DATE
Storage of DATE variables is based on a 4 byte integer representing the number of days since 1899/12/31.
The value range is from 0001-01-1 (-693594) to 9999-12-31 (2958464) .
Because DATE values are stored as integers, you can use them in arithmetic expressions: the difference of two dates returns the number of days. This is possible (and portable) with language arithmetic operators, but should be avoided in SQL statements, because not all databases support integer-based date arithmetic.
Data conversions, input and display of DATE values are ruled by environment settings, such as the DBDATE and DBCENTURY environment variables.
Several built-in functions and constants are available such as MDY() and TODAY.
DATE variables are initialized to zero (=1899/12/31) in functions, modules and globals.
Integers can represent dates as a number of days starting from 1899/12/31, and can be assigned to dates. It is not recommended that you directly assign integers to dates, however, for source code readability.As Date-to-String conversion is based on an environment setting, it is not recommended that you hardcode strings representing Dates.
01
MAIN02
DEFINE d DATE03
LET d = TODAY04
DISPLAY d, " ", d+10005
END MAIN
The DATETIME data type stores date and time data with time units from the year to fractions of a second.
DATETIME qual1 TO qual2
where qual1 can be one of:
YEAR
MONTH
DAY
HOUR
MINUTE
SECOND
FRACTION
and qual2 can be one of:
YEAR
MONTH
DAY
HOUR
MINUTE
SECOND
FRACTION
FRACTION(1)
FRACTION(2)
FRACTION(3)
FRACTION(4)
FRACTION(5)
The DATETIME data type stores an instance in time, expressed as a calendar date and time-of-day.
The qual1 and qual2 qualifiers define the precision of the DATETIME variable. The precision can range from a year through a fraction of second.
DATETIME arithmetic is based on the INTERVAL data type, and can be combined with DATE values:
Left Operand Type | Operator | Right Operand Type | Result Type |
DATETIME | - | DATETIME | INTERVAL |
DATETIME | - | DATE | INTERVAL |
DATETIME | - | INTERVAL | DATETIME |
DATETIME | + | INTERVAL | DATETIME |
The CURRENT operator provides current system date/time.
You can assign DATETIME variables with datetime literals with a specific notation.
DATETIME variables are initialized to NULL in functions, modules and globals.
DATETIME values can be converted to strings by the ISO format (YYYY-MM-DD hh:mm:ss.fffff).
01
MAIN02
DEFINE d1, d2 DATETIME YEAR TO MINUTE03
LET d1 = CURRENT YEAR TO MINUTE04
LET d2 = "1998-01-23 12:34"05
DISPLAY d1, d206
END MAIN
The INTERVAL data type stores spans of time as Year/Month or Day/Hour/Minute/Second/Fraction units.
INTERVAL YEAR[(precision)]
TO MONTH
|INTERVAL YEAR[(precision)] TO YEAR
|INTERVAL MONTH[(precision)] TO MONTH
INTERVAL DAY[(precision)]
TO FRACTION[(scale)]
|INTERVAL DAY[(precision)] TO
SECOND
|INTERVAL DAY[(precision)] TO
MINUTE
|INTERVAL DAY[(precision)] TO
HOUR
|INTERVAL DAY[(precision)] TO
DAY
|INTERVAL HOUR[(precision)] TO
FRACTION[(scale)]
|INTERVAL HOUR[(precision)] TO
SECOND
|INTERVAL HOUR[(precision)] TO
MINUTE
|INTERVAL HOUR[(precision)] TO HOUR
|INTERVAL MINUTE[(precision)] TO FRACTION[(scale)]
|INTERVAL MINUTE[(precision)] TO SECOND
|INTERVAL MINUTE[(precision)] TO MINUTE
|INTERVAL SECOND[(precision)] TO FRACTION[(scale)]
|INTERVAL SECOND[(precision)] TO SECOND
|INTERVAL FRACTION TO FRACTION[(scale)]
The INTERVAL data type stores a span of time, the difference between two points in time. It can also be used to store quantities that are measured in units of time, such as ages or times required for some activity.
The INTERVAL data type falls in two classes, which are mutually exclusive:
INTERVAL values can be negative.
INTERVAL arithmetic is possible, and can involve DATETIME as well as DECIMAL operands:
Left Operand Type | Operator | Right Operand Type | Result Type |
INTERVAL | * | DECIMAL | INTERVAL |
INTERVAL | / | DECIMAL | INTERVAL |
INTERVAL | - | INTERVAL | INTERVAL |
INTERVAL | + | INTERVAL | INTERVAL |
DATETIME | - | INTERVAL | DATETIME |
DATETIME | + | INTERVAL | DATETIME |
DATETIME | - | DATETIME | INTERVAL |
You can assign INTERVAL variable with interval literals with a specific notation.
INTERVAL values can be converted to strings by using the ISO format (YYYY-MM-DD hh:mm:ss.fffff).
01
MAIN02
DEFINE i1 INTERVAL YEAR TO MONTH03
DEFINE i2 INTERVAL DAY(5) TO MINUTE04
LET i1 = "2342-4"05
LET i2 = "23423 12:34"06
DISPLAY i1, i207
END MAIN
The BYTE data type stores any type of binary data, such as images or sounds.
BYTE
Keep in mind that a BYTE variable must be initialized with the LOCATE instruction before usage. You might want to free resources allocated to the BYTE variable with the FREE instruction. Note that a FREE will remove the file if the LOB variable is located in a file.
The LOCATE instruction basically defines where the large data object has to be stored (in file or memory). This instruction will actually allow you to fetch a LOB into memory or into a file, or insert a LOB from memory or from a file into the database.
When you assign a BYTE variable to another BYTE variable, the LOB data is not duplicated, only the handler is copied.
Note that if you need to clone the large object, you can use the I/O built-in methods to read/write data from/to a specific file. The large object can be located in memory, in a specific file or in a temporary file (the temp dir can be defined by DBTEMP).
Object Methods | |
Name | Description |
readFile( fileName STRING ) |
Reads data from a file and copies into memory or to the file used by the variables according to the LOCATE statement issued on the object. |
writeFile( fileName STRING ) |
Writes data from the variable (memory or source file) to the destination file passed as parameter. The file is created if it does not exist. |
01
MAIN02
DEFINE b BYTE03
DATABASE stock04
LOCATE b IN MEMORY05
SELECT png_image INTO b FROM images WHERE image_id = 12306
CALL b.writeFile("/tmp/image.png")07
END MAIN
The TEXT data type stores large text data.
TEXT
Keep in mind that a TEXT variable must be initialized with the LOCATE instruction before usage. You might want to free resources allocated to the TEXT variable with the FREE instruction. Note that a FREE will remove the file if the LOB variable is located in a file.
The LOCATE instruction basically defines where the large data object has to be stored (in file or memory). This instruction will actually allow you to fetch a LOB into memory or into a file, or insert a LOB from memory or from a file into the database.
When you assign a TEXT variable to another TEXT variable, the LOB data is not duplicated, only the handler is copied.
You can assign TEXT variables to/from VARCHAR, CHAR and STRING variables.
Note that if you need to clone the large object, you can use the I/O built-in methods to read/write data from/to a specific file. The large object can be located in memory, in a specific file or in a temporary file (the temp dir can be defined by DBTEMP).
Object Methods | |
Name | Description |
getLength() |
Returns the size of the text in bytes. |
readFile( fileName STRING ) |
Reads data from a file and copies into memory or to the file used by the variables according to the LOCATE statement issued on the object. |
writeFile( fileName STRING ) |
Writes data from the variable (memory or source file) to the destination file passed as parameter. The file is created if it does not exist. |
01
MAIN02
DEFINE t TEXT03
DATABASE stock04
LOCATE t IN FILE "/tmp/mytext.txt"05
SELECT doc_text INTO t FROM documents WHERE doc_id = 12306
CALL t.writeFile("/tmp/document.txt")07
END MAIN
The BOOLEAN data type stores a logical value, TRUE or FALSE.
BOOLEAN
Boolean data types have two possible values: TRUE (1) and FALSE (0). Variables of this type can be used to store the result of a boolean expression.
The BOOLEAN type can be used to define variables used in SQL statements, and can store values of IBM Informix BOOLEAN columns.
Note that while IBM Informix SQL BOOLEAN type accepts the 't' and 'f' values, the Genero BDL BOOLEAN type expects 0/FALSE and 1/TRUE integer values only. You can however insert/ fetch a BOOLEAN variable into/from a BOOLEAN column.01
FUNCTION checkOrderStatus( cid )02
DEFINE oid INT, b BOOLEAN03
LET b = ( isValid(oid) AND isStored(oid) )04
IF NOT b THEN05
ERROR "The order is not ready."06
END IF07
END FUNCTION