INFORMIX
Informix Guide to SQL: Reference
Chapter 2: Data Types
Home Contents Index Master Index New Book

Built-In Data Types

Universal Server provides the following built-in data types:

Character data types:

CHAR, CHARACTER VARYING, LVARCHAR, NCHAR, NVARCHAR

Numeric data types:

DECIMAL, MONEY, SMALLINT, INTEGER, INT8,
SERIAL, SERIAL8

Large-object data types:

Simple-large-object types: TEXT, BYTE

Smart-large-object types: CLOB, BLOB

Time data type:

DATE, DATETIME, INTERVAL

Miscellaneous data types:

BOOLEAN

For a description of each of these data types, refer to the appropriate entry in "Supported Data Types".

GLS
The NCHAR and NVARCHAR data types are also character data types.

For information on how to use time data types, see "Using DATE, DATETIME, and INTERVAL Data".

Using DATE, DATETIME, and INTERVAL Data

You can use DATE, DATETIME, and INTERVAL data in a variety of arithmetic and relational expressions. You can manipulate a DATETIME value with another DATETIME value, an INTERVAL value, the current time (identified by the keyword CURRENT), or a specified unit of time (identified by the keyword UNITS). In most situations, you can use a DATE value wherever it is appropriate to use a DATETIME value and vice versa. You also can manipulate an INTERVAL value with the same choices as a DATETIME value. In addition, you can multiply or divide an INTERVAL value by a number.

An INTERVAL column can hold a value that represents the difference between two DATETIME values or the difference between (or sum of) two INTERVAL values. In either case, the result is a span of time, which is an INTERVAL value. On the other hand, if you add or subtract an INTERVAL value from a DATETIME value, another DATETIME value is produced because the result is a specific time.

Figure 2-3 indicates the range of expressions that you can use with DATE, DATETIME, and INTERVAL data, along with the data type that results from each expression.

Figure 2-3
Range of Expressions for DATE, DATETIME, and INTERVAL

(1 of 2)

Data Type of Operand 1 Operator Data Type of Operand 2 Result

DATE

-

DATETIME

INTERVAL

DATETIME

-

DATE

INTERVAL

DATE

+ or -

INTERVAL

DATETIME

DATETIME

-

DATETIME

INTERVAL

DATETIME

+ or -

INTERVAL

DATETIME

INTERVAL

+

DATETIME

DATETIME

INTERVAL

+ or -

INTERVAL

INTERVAL

DATETIME

-

CURRENT

INTERVAL

CURRENT

-

DATETIME

INTERVAL

INTERVAL

+

CURRENT

DATETIME

CURRENT

+ or -

INTERVAL

DATETIME

DATETIME

+ or -

UNITS

DATETIME

INTERVAL

+ or -

UNITS

INTERVAL

INTERVAL

* or /

NUMBER

INTERVAL

No other combinations are allowed. You cannot add two DATETIME values because this operation does not produce either a specific time or a span of time. For example, you cannot add December 25 and January 1, but you can subtract one from the other to find the time span between them.

Manipulating DATETIME Values

You can subtract most DATETIME values from each other. Dates can be in any order and the result is either a positive or a negative INTERVAL value. The first DATETIME value determines the field precision of the result.

If the second DATETIME value has fewer fields than the first, the shorter value is extended automatically to match the longer one. (See the discussion of the EXTEND function in the Expression segment in Chapter 1 of the Informix Guide to SQL: Syntax.) In the following example, subtracting the DATETIME YEAR TO HOUR value from the DATETIME YEAR TO MINUTE value results in a positive interval value of 60 days, 1 hour, and 30 minutes. Because minutes were not included in the second value, the database server sets the minutes for the result to 0.

If the second DATETIME value has more fields than the first (regardless of whether the precision of the extra fields is larger or smaller than those in the first value), the additional fields in the second value are ignored in the calculation.

In the following expression (and result), the year is not included for the second value. Therefore, the year is set automatically to the current year, in this case 1994, and the resulting INTERVAL is negative, indicating that the second date is later than the first.

Manipulating DATETIME with INTERVAL Values

INTERVAL values can be added to or subtracted from DATETIME values. In either case, the result is a DATETIME value. If you are adding an INTERVAL value to a DATETIME value, the order of values is unimportant; however, if you are subtracting, the DATETIME value must come first. Adding or subtracting an INTERVAL value simply moves the DATETIME value forward or backward in time. The expression shown in the following example moves the date ahead three years and five months:

Important: Evaluate the logic of your addition or subtraction. Remember that months can be 28, 29, 30, or 31 days and that years can be 365 or 366 days.
In most situations, the database server automatically adjusts the calculation when the initial values do not have the same precision. However, in certain situations, you must explicitly adjust the precision of one value to perform the calculation. If the INTERVAL value you are adding or subtracting has fields that are not included in the DATETIME value, you must use the EXTEND function to explicitly extend the field qualifier of the DATETIME value. (For more information on the EXTEND function, see the Expression segment in the Informix Guide to SQL: Syntax.) For example, you cannot subtract a minute INTERVAL value from the DATETIME value in the previous example that has a YEAR TO DAY field qualifier. You can, however, use the EXTEND function to perform this calculation, as shown in the following example:

The EXTEND function allows you to explicitly increase the DATETIME precision from YEAR TO DAY to YEAR TO MINUTE. This allows the database server to perform the calculation, with the resulting extended precision of YEAR TO MINUTE.

Manipulating DATE with DATETIME and INTERVAL Values

You can use DATE values in arithmetic expressions with DATETIME or INTERVAL values by writing expressions that allow the manipulations that Figure 2-4 shows.

Figure 2-4
Results of Expressions That Manipulate DATE with DATETIME or INTERVAL Values

Expression Result

DATE - DATETIME

INTERVAL

DATETIME - DATE

INTERVAL

DATE + or - INTERVAL

DATETIME

In the cases that Figure 2-4 shows, DATE values are first converted to their corresponding DATETIME equivalents, and then the expression is computed normally.

Although you can interchange DATE and DATETIME values in many situations, you must indicate whether a value is a DATE or a DATETIME data type. A DATE value can come from the following sources:

  • A column or program variable of type DATE
  • The TODAY keyword
  • The DATE() function
  • The MDY function
  • A DATE literal
A DATETIME value can come from the following sources:

  • A column or program variable of type DATETIME
  • The CURRENT keyword
  • The EXTEND function
  • A DATETIME literal
The database locale defines the default DATE and DATETIME formats. For the default locale, U.S. English, these formats are 'mm/dd/yy' for DATE values and 'yyyy-mm-dd hh:MM:ss' for DATETIME values.

When you represent DATE and DATETIME values as quoted character strings, the fields in the strings must be in proper order. In other words, when a DATE value is expected, the string must be in DATE format and when a DATETIME value is expected, the string must be in DATETIME format. For example, you can use the string '10/30/1994' as a DATE string but not as a DATETIME string. Instead, you must use '1994-10-30' or '94-10-30' as the DATETIME string.

GLS
If you use a non-default locale, the DATE and DATETIME strings must match the formats that your locale defines. For more information, see the Guide to GLS Functionality.

You can customize the DATE format that the database server expects with the DBDATE and GL_DATE environment variables. You can customize the DATETIME format that the database server expects with the DBTIME and GL_DATETIME environment variables. For more information on these environment variables, see the Guide to GLS Functionality.

You also can subtract one DATE value from another DATE value, but the result is a positive or negative INTEGER value rather than an INTERVAL value. If an INTERVAL value is required, you can either convert the INTEGER value into an INTERVAL value or one of the DATE values into a DATETIME value before subtracting.

For example, the following expression uses the DATE() function to convert character string constants to DATE values, calculates their difference, and then uses the UNITS DAY keywords to convert the INTEGER result into an INTERVAL value:

If you need YEAR TO MONTH precision, you can use the EXTEND function on the first DATE operand, as shown in the following example:

The resulting INTERVAL precision is YEAR TO MONTH because the DATETIME value came first. If the DATE value had come first, the resulting INTERVAL precision would have been DAY(5) TO DAY.

Manipulating INTERVAL Values

You can add or subtract INTERVAL values as long as both values are from the same class; that is, both are year-month or both are day-time. In the following example, a SECOND TO FRACTION value is subtracted from a MINUTE TO FRACTION value:

Note the use of numeric qualifiers to alert the database server that the MINUTE and FRACTION in the first value and the SECOND in the second value exceed the default number of digits.

When you add or subtract INTERVAL values, the second value cannot have a field with greater precision than the first. The second INTERVAL, however, can have a field of smaller precision than the first. For example, the second INTERVAL can be HOUR TO SECOND when the first is DAY TO HOUR. The additional fields (in this case MINUTE and SECOND) in the second INTERVAL value are ignored in the calculation.

Multiplying or Dividing INTERVAL Values

You can multiply or divide INTERVAL values by a number that can be an integer or a fraction. However, any remainder from the calculation is ignored and the result is truncated. The following expression multiplies an INTERVAL by a fraction:

In this example, 15 * 2.5 = 37.5 minutes, 30 * 2.5 = 75 seconds, and 2 * 2.5 = 5 fraction(4). The 0.5 minute is converted into 30 seconds and 60 seconds are converted into 1 minute, which produces the final result of 38 minutes, 45 seconds, and 0.0005 of a second. The results of any calculation include the same amount of precision as the original INTERVAL value.

Large-Object Data Types

A large object is a data object that is logically stored in a table column but physically stored independently of the column. Large objects are stored separately from the table because they typically store a large amount of data. Separation of this data from the table can increase performance.

Figure 2-5 shows the different large-object data types that Universal Server supports.

Figure 2-5
Large-Object Data Types Supported by Universal Server

For the relative advantages and disadvantages of using simple and smart large objects, see Chapter 9, "Implementing Your Data Model," of the Informix Guide to SQL: Tutorial.

Smart Large Objects

Smart large objects are a category of large objects that support random access to the data and are generally recoverable. The random access feature allows you to seek and read through the smart large object as if it were an operating-system file. Smart large objects are also useful for opaque data types with large storage requirements. (See the description of opaque data types on page 2-24.)

Universal Server supports the following smart-large-object data types:

BLOB

Stores binary data. For more information about this data type, see the description on page 2-33.

CLOB

Stores text data. For more detailed information about this data type, see the description on page 2-38.

Universal Server stores smart large objects in sbspaces. An sbspace is a logical storage area that contains one or more chunks that only store BLOB and CLOB data. For information on how to define sbspaces, see the INFORMIX-Universal Server Administrator's Guide.

When you define a BLOB or CLOB column, you can determine the following large-object characteristics:

  • LOG and NOLOG: whether the database server should log the smart large object in accordance with the current database log mode.
  • KEEP ACCESS TIME and NO KEEP ACCESS TIME: whether the database server should keep track of the last time the smart large object was accessed.
  • HIGH INTEG and MODERATE INTEG: whether the database server should use page headers to detect data corruption.
Use of these characteristics can affect performance. For more information, see the INFORMIX-Universal Server Administrator's Guide.

When you access a smart-large-object column with an SQL statement, the database server does not send the actual BLOB or CLOB data. Instead, it establishes a pointer to the data and returns this pointer. The client application can then use this pointer to perform the open, read, or write operations on the smart large object.

To access a BLOB or CLOB column from within a client application, use one of the following application programming interfaces (APIs):

  • From within an INFORMIX-ESQL/C program, use the smart-large-object API.
    For more information, see the INFORMIX-ESQL/C Programmer's Manual.

  • From within a DataBlade module, use the Client and Server API.
    For more information, see the INFORMIX-Universal Server DataBlade API Guide.

For information on using smart large objects, see Chapter 9, "Implementing Your Data Model," of the Informix Guide to SQL: Tutorial and the Data Type and Expression segments in the Informix Guide to SQL: Syntax.

Simple Large Objects

Simple large objects are a category of large objects that have a theoretical limit of 231 bytes and a practical limit determined by your disk capacity. Universal Server supports the following simple-large-object data types:

BYTE

Stores binary data. For more detailed information about this data type, see the description on page 2-35.

TEXT

Stores text data. For more detailed information about this data type, see the description on page 2-65.

Tip: In INFORMIX-OnLine Dynamic Server, the TEXT and BYTE data types are collectively called "binary large objects" or just "blobs." Universal Server uses the term "simple large object" to refer to TEXT and BYTE to distinguish these data types from the CLOB and BLOB data types.
Unlike smart large objects (see page 2-17), simple large objects do not support random access to the data. When you transfer a simple large object between a client application and the database server, you must transfer the entire BYTE or TEXT value. If the data does not fit into memory, you must store it in an operating-system file and then retrieve it from that file.

Universal Server stores simple large objects in blobspaces. A blobspace is a logical storage area that contains one or more chunks that only store BYTE and TEXT data. For information on how to define blobspaces, see the INFORMIX-Universal Server Administrator's Guide.




Informix Guide to SQL: Reference, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.