DB2 12 - Data types - Date, time, and timestamp data types


Volta a página anterior

Volta ao Menu Principal


Desenvolvido por DORNELLES Carlos Alberto - Analista de Sistemas - Brasília DF. - cad_cobol@hotmail.com

DB2 12 - Data types - Date, time, and timestamp data types

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

Data type Denotes a column of...
DATE A date is a three-part value representing a year, month, and day in the range of 0001-01-01 to 9999-12-31.
TIME 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.
TIMESTAMP 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

Format name Abbreviation Typical date Typical time
International Standards Organization ISO 2003-12-25 13.30.05
IBM® USA standard USA 12/25/2003 1:30 PM
IBM European standard EUR 25.12.2003 13.30.05
Japanese Industrial Standard Christian Era JIS 2003-12-25 13:30:05

Example 1:
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.

Example 2:
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.