The SQL DATETIME and INTERVAL data types
- The datetime data type, which encodes an instant in time as a calendar date and a time of day.
- The interval data type, which encodes a span of time.
SQL data type | ESQL/C data type | C typedef name | Sample declaration |
---|---|---|---|
DATETIME | datetime | dtime_t | EXEC SQL BEGIN DECLARE SECTION; datetime year to day sale; EXEC SQL END DECLARE SECTION; |
INTERVAL | interval | intrvl_t | EXEC SQL BEGIN DECLARE SECTION; interval hour to second test_num; EXEC SQL END DECLARE SECTION; |
EXEC SQL include datetime;
The decimal.h header file defines the type dec_t, which is a component of the dtime_t and intrvl_t structures.
EXEC SQL BEGIN DECLARE SECTION;
datetime year; /* will cause an error */
datetime year to day year, today; /* ambiguous */
EXEC SQL END DECLARE SECTION;
A datetime or interval data type is stored as a decimal number with a scale factor of zero and a precision equal to the number of digits that its qualifier implies. When you know the precision and scale, you know the storage format. For example, if you define a table column as DATETIME YEAR TO DAY, it contains four digits for year, two digits for month, and two digits for day, for a total of eight digits. It is thus stored as decimal(8,0).
interval day(3) to day;
For more information about the DATETIME and INTERVAL data types, see the HCL OneDB™ Guide to SQL: Reference.