Although storing dates and times as numeric values is possible, using datetime data types is recommended.
The datetime data types are DATE, TIME, and TIMESTAMP.
The following table describes the data types for dates, times, and timestamps.
Table 1. Date, time, and timestamp data types
|Denotes a column of...
|A date is a three-part value representing a year, month, and day in the range of
0001-01-01 to 9999-12-31.
|A time is a three-part value representing a time of day in hours, minutes, and seconds,
in the range of 00.00.00 to 24.00.00.
|A timestamp is a seven-part value representing a date and time by year, month, day,
hour, minute, second, and microsecond,
in the range of 0001-01-01-00.00.00.000000000 to 9999-12-31-24.00.00.000000000 with nanosecond precision. Timestamps can also hold timezone information.
Db2® stores values of datetime data types in a special internal format.
When you load or retrieve data, Db2 can convert it to or from any of the formats in the following table.
Table 2. Date and time format options
|International Standards Organization
|IBM® USA standard
|IBM European standard
|Japanese Industrial Standard Christian Era
The following query displays the dates on which all employees were hired, in IBM USA standard form, regardless of the local default:
SELECT EMPNO, CHAR(HIREDATE, USA) FROM EMP;
When you use datetime data types, you can take advantage of Db2 built-in functions that operate specifically on datetime values, and you can specify
calculations for datetime values.
Assume that a manufacturing company has an objective to ship all customer orders within five days.
You define the SHIPDATE and ORDERDATE columns as DATE data types.
The company can use datetime data types and the DAYS built-in function to compare the shipment date to the order date.
Here is how the company might code the function to generate a list of orders that have exceeded the five-day shipment objective:>
DAYS(SHIPDATE) - DAYS(ORDERDATE)> 5
As a result, programmers do not need to develop, test, and maintain application code to perform complex datetime arithmetic that needs to allow for the number
of days in each month.
You can use the following sample user-defined functions (which come with Db2) to modify the way dates and times are displayed.
- ALTDATE returns the current date in a user-specified format or converts a user-specified date from one format to another.
- ALTTIME returns the current time in a user-specified format or converts a user-specified time from one format to another.
At installation time, you can also supply an exit routine to make conversions to and from any local standard.
When loading date or time values from an outside source, Db2 accepts any of the date and time format options that are listed in this information.
Db2 converts valid input values to the internal format.
For retrieval, a default format is specified at Db2 installation time.
You can subsequently override that default by using a precompiler option for all statements in a program or by using the scalar function CHAR for a particular
SQL statement and by specifying the format that you want.
© Copyright IBM Corp.