String representations of datetime values

Dates, times, and timestamp values can be represented by strings. For many host languages, there are no special SQL constants for datetime values and, except for Java, no host variables with a data type of date, time, or timestamp. Thus, to be retrieved, a datetime value must be assigned to a string variable.

Start of changeValues whose data types are DATE, TIME, TIMESTAMP WITHOUT TIME ZONE, or TIMESTAMP WITH TIME ZONE are represented in a form that is transparent to the user of SQL. Dates, times, and timestamps (with or without time zones) can also be represented by strings. These representations directly concern the SQL user because, for many host languages there are no special SQL constants or host variables with a data type for DATE, TIME, TIMESTAMP WITHOUT TIME ZONE, or TIMESTAMP WITH TIME ZONE values (for variables with Java). Thus, to be retrieved, a datetime value must be assigned to a string variable. The format of the resulting string depends on the default date format and the default time format that is in effect when the statement is prepared.End of change

Each datetime value is assigned an encoding scheme. This encoding scheme is used when the datetime value is converted from its internal form to the string representation in the form of the mixed CCSID if the field MIXED DATA is YES on installation panel DSNTIPF. Otherwise the SBCS CCSID of the assigned encoding scheme is used. For Unicode, the mixed CCSID is always used. The following table shows how the encoding scheme is determined:

Table 1. The encoding scheme of datetime values
Datetime expression Result encoding scheme
Columns The same encoding scheme as the table that contains the column
Host variables If the statement references:
  • A single encoding scheme - The same encoding scheme
  • Multiple encoding schemes - The application encoding scheme
Special registers If the statement references:
  • A single encoding scheme - The same encoding scheme
  • Multiple encoding schemes - The application encoding scheme
Expressions If the statement references:
  • A single encoding scheme - The same encoding scheme
  • Multiple encoding schemes - The application encoding scheme

For ASCII and EBCDIC, a string representation of a datetime value must be a character string. For Unicode, a string representation of a datetime value can be either a character string or a graphic string. Thus, the only time a graphic string can be used for a datetime value is when the encoding scheme is Unicode.

In host languages other than Java, a datetime value must be assigned to a string variable. When a date or time is assigned to a string variable, the string format is determined by a precompiler option or subsystem parameter. When a string representation of a datetime value is used in other operations, it is converted to a datetime value. However, this can be done only if the string representation is recognized by DB2® or an exit provided by the installation and the other operand is a compatible datetime value. An input string representation of a date or time with LOCAL specified must have an actual length that is not greater than 255 bytes.

Start of changeDatetime values that are represented by strings can appear in contexts that require values whose data types are DATE, TIME, TIMESTAMP WITHOUT TIME ZONE, or TIMESTAMP WITH TIME ZONE. A string representation of a date, time or timestamp (with or without time zone) can be passed as an argument to the DATE, TIME, TIMESTAMP, or TIMESTAMP_TZ function to obtain a datetime value. A CAST specification can also be used to turn a character representation of a date, time, or timestamp (with or without time zone) into a datetime value.End of change

Date strings:
A string representation of a date is a string that starts with a digit and has a length of at least 8 characters. Trailing blanks can be included, leading blanks are not allowed, and leading zeros can be omitted in the month and day portions.

The following table shows the valid string formats for dates. Each format is identified by name and includes an associated abbreviation (for use by the CHAR function) and an example of its use. For an installation-defined date string format, the format and length must have been specified when DB2 was installed. They cannot be listed here.

Table 2. Formats for string representations of dates
Format name Abbreviation Date format Example
International Standards Organization ISO yyyy-mm-dd 1987-10-12
IBM® USA standard USA mm/dd/yyyy 10/12/1987
IBM European standard EUR dd.mm.yyyy 12.10.1987
Japanese industrial standard Christian era JIS yyyy-mm-dd 1987-10-12
Installation-defined LOCAL Any installation-defined form
Time strings:
A string representation of a time is a string that starts with a digit, and has a length of at least 4 characters. Trailing blanks can be included, leading blanks are not allowed, and leading zeros can be omitted in the hour part of the time; seconds can be omitted entirely. If you choose to omit seconds, an implicit specification of 0 seconds is assumed. Thus 13.30 is equivalent to 13.30.00.

The following table shows the valid string formats for times. Each format is identified by name and includes an associated abbreviation (for use by the CHAR function) and an example of its use. In the case of an installation-defined time string format, the format and length must have been specified when your DB2 subsystem was installed. They cannot be listed here.

Table 3. Formats for string representations of times
Format name Abbreviation Time format Example
International Standards Organization 1 ISO1 hh.mm.ss 13.30.05
IBM USA standard USA hh:mm AM or PM 1:30 PM
IBM European standard EUR hh.mm.ss 13.30.05
Japanese industrial standard Christian era JIS hh:mm:ss 13:30:05
Installation-defined LOCAL Any installation-defined form
Note: 1. This is an earlier version of the ISO format. JIS can be used to get the current ISO format.

In the USA format:

  • The minutes can be omitted, thereby specifying 00 minutes. For example, 1 PM is equivalent to 1:00 PM.
  • The letters A, M, and P can be lowercase.
  • A single blank must precede the AM or PM.
  • The hour must not be greater than 12 and cannot be 0 except for the special case of 00:00 AM.

Using the ISO format of the 24-hour clock, the correspondence between the USA format and the 24-hour clock is as follows:

  • 12:01 AM through 12:59 AM correspond to 00.01.00 through 00.59.00
  • 01:00 AM through 11:59 AM correspond to 01.00.00 through 11.59.00
  • 12:00 PM (noon) through 11:59 PM correspond to 12.00.00 through 23.59.00
  • 12:00 AM (midnight) corresponds to 24.00.00
  • 00:00 AM (midnight) corresponds to 00.00.00
Start of changeTimestamp strings:End of change
Start of changeA string representation of a timestamp is a character or graphic string that starts with a digit and has a length of at least 16 characters.

The character or graphic string must contain a value that conforms to one of the formats listed in Datetime constants, subject to the following rules:

  • leading blanks are not allowed
  • trailing blanks can be included
  • leading zeros can be omitted from the month, day, hour, and time zone hour elements of the timestamp. An implicit specification of 0 is assumed for any digit that is omitted.
  • the hour can be 24 if the minutes, seconds, and any fractional seconds are all zeroes.
  • leading zeros must be included for the minute, second, and time zone minute elements of the timestamp.
  • the number of digits of fractional seconds can vary from 0 to 12. An implicit specification of 0 is assumed if fractional seconds are omitted.
  • the separator character that follows the seconds element can be omitted if fractional seconds are not included.
  • an optional single blank can be included between the time and the time zone.
  • an optional time zone can be included, in one of the following formats:
    • ±th:tm, with values ranging from -24:00 to +24:00. A value of -0:00 is treated the same as a value of +0:00.
    • ±th, with values ranging from -24 to +24, and an implicit specification of 00 is assumed for the time zone minute element.
    • uppercase Z to specify UTC

If a string representation of a timestamp is implicitly cast to a value with a timestamp data type, the timestamp precision is assumed to be 6, regardless of the number of digits of fractional seconds in the string. Beyond the sixth digit that represents fractional seconds, the digits are truncated and the missing digits are assumed to be zeros. For example, 1990-3-2-8.30.00.10 is equivalent to 1990-03-02-08.30.00.100000. A string representation of a timestamp can be given a different timestamp precision by explicitly casting the value to a timestamp with a specified precision or, in the case of a constant, preceding the string with the TIMESTAMP keyword (for example, TIMESTAMP 2007-03-28-14.50.35.123; has the TIMESTAMP(3) data type).

If a string representation of a timestamp is implicitly cast to a TIMESTAMP WITHOUT TIME ZONE value, the string must not contain a time zone.

SQL statements also support the ODBC or JDBC string representation of a timestamp as an input value only. The ODBC and JDBC string representation of a timestamp has the form yyyy-mm-dd hh:mm:ss.nnnnnn.

End of change

LOCAL date and time exits: For LOCAL, the date exit for ASCII data is DSNXVDTA, the date exit for EBCDIC is DSNXVDTX, and the date exit for Unicode is DSNXVDTU. For LOCAL, the time exit for ASCII data is DSNXVTMA, the time exit for EBCDIC is DSNXVTMX, and the time exit for Unicode is DSNXVTMU.