TIMESTAMP_FORMAT

Start of changeThe TIMESTAMP_FORMAT function returns a TIMESTAMP WITHOUT TIME ZONE value that is based on the interpretation of the input string using the specified format.End of change

Read syntax diagram
                                                             .-,--6------------------.      
>>-TIMESTAMP_FORMAT--(--string-expression--,--format-string--+-----------------------+--)-><
                                                             '-,--precision-constant-'      

The schema is SYSIBM.

string-expression
Start of changeAn expression that returns a value of any built-in character or graphic string data type, other than a CLOB or DBCLOB, with an actual length that is not greater than 255 bytes. The string-expression must contain the components of a timestamp that correspond to the format that is specified in format-string, except for hour, minute, second, or fractional seconds.End of change
format-string
Start of changeThe expression must return a value that is a built-in character or graphic string data type, other than a CLOB or DBCLOB, with a length attribute that is not greater than 255 bytes. The actual length must not be greater than 255 bytes.End of change The value is a template for how string-expression is interpreted and then converted to a timestamp value.

A valid format-string must contain at least one format element, must not contain multiple specifications for any component of a timestamp, and can contain any combination of the format elements, unless otherwise noted in the following table. For example, format-string cannot contain both YY and YYYY, because both are used to interpret the year component of a string-expression. Two format elements can be separated by one or more of the following separator characters:

  • minus sign (-)
  • period (.)
  • forward slash (/)
  • comma (,)
  • apostrophe (')
  • semicolon (;)
  • colon (:)
  • blank ( )

Separator characters can also be specified at the start or end of format-sting. These separator characters can be used in any combination in the format string, for example 'YYYY/MM-DD HH:MM.SS'. Separator character that is specified in a string-expression are used to separate components and are not required to match the separator character that is specified in the format-string.

Table 1. Format elements for the TIMESTAMP_FORMAT function
Format element Related component of a timestamp Description
AM or PM 1 hour Meridian indicator (morning or evening) without periods. This format element uses the exact strings "AM" or "PM".
Start of changeA.M. or P.M. 1End of change Start of changehourEnd of change Start of changeMeridian indicator (morning or evening) with periods. This format element uses the exact strings "A.M." or "P.M."End of change
Start of changeD 1End of change Start of changenoneEnd of change Start of changeDay of the week (1-7).End of change
DD day Day of the month (0-31).
Start of changeDDDEnd of change Start of changemonth, dayEnd of change Start of changeDay of the year (001-366).End of change
Start of changeFF or FFnEnd of change Start of changefractional secondsEnd of change Start of changeFractional seconds (0-999999999999). The number n is used to specify the number of digits that is expected in the string-expression. Valid values for n are 1-12 with no leading zeros.

Specifying FF is equivalent to specifying FF6. When the component in string-expression that corresponds to the FF format element is followed by a separator character or is the last component, the number of digits for the fractional seconds can be less than what is specified by the format element. In this case, zero digits are padded onto the right of the number of specified digits.

End of change
HH hour HH behaves the same as HH12.
HH12 hour Hour of the day (01-12) in 12-hour format. AM is the default meridian indicator.
HH24 hour Hour of the day (00-24) in 24-hour format.
Start of changeJEnd of change Start of changeyear, month, and dayEnd of change Start of changeJulian day (number of days since January 1, 4713 BC).End of change
MI minute Minute (00-59).
MM month Month (01-12).
MONTH, Month, or month 1, 2 month Name of the month in English.
MON, Mon, or mon 1, 2 month Abbreviated name of the month in English.
NNNNNN microseconds Microseconds (000000-999999).
RR year Last two digits of the adjusted year (00-99).
RRRR year Four digit adjusted year (0000-9999).
SS seconds Seconds (00-59).
Start of changeSSSSSEnd of change Start of changehours, minutes, and secondsEnd of change Start of changeSeconds since the previous midnight (00000 - 86400).End of change
Y year Last digit of the year (0-9). First three digits of the current year are used to determine the full 4-digit year.
YY year Last two digits of the year (00-99). First two digits of the current year are used to determine the full 4-digit year.
YYY year Last three digits of the year (000-999). First digit of the current year is used to determine the full 4-digit year.
YYYY year 4-digit year (0000-9999).
Notes:
  1. This format element is case sensitive.
  2. Only these exact spellings and case combinations can be used. If this format element is specified in an invalid case combination an error is returned.
  3. Start of changeThe D format element does not contribute to any components of the resulting timestamp. However, a specified value for this format element must be correct for the combination of the day component of the resulting timestamp. For example, a value of '5' for string-expression is valid for a format string value of 'D'. However, value of '9' for string-expression would result in an error for the same format-string.End of change
The RR and RRRR format elements can be used to change how a specification for a year is to be interpreted by adjusting the value to produce a 2-digit or a 4-digit value depending on the leftmost two digits of the current year according to the following table:
Table 2. Correspondence of adjusted year value and timestamp component
Digits of the current year Two-digit year in string-expression First two digits of the year component of timestamp
00-50 00-49 First two digits of the current year
51-99 00-49 First two digits of the current year + 1
00-50 50-99 First two digits of the current year -1
51-99 50-99 First two digits of the current year

For example, if the current year is 2007, '86' with format 'RR' means 1986, but if the current year is 2052, it means 2086.

The following defaults are used when a format-string does not include a format element for one of the following components of a timestamp:
Timestamp component Default
year current year, as 4 digits
month current month. as 2 digits
day 01 (first day of the month)
hour 00
minute 00
second 00
fractional seconds a number of zeros to match the timestamp precision of the result

If string-expression does not include a value that corresponds to an hour, minute, second, or fractional seconds format element that is specified in the format-string, the same defaults are used.

Leading zeros can be specified for any component of the timestamp value (that is, month, day, hour, minutes, seconds) that does not have the maximum number of significant digits for the corresponding format element in the format-string.

A substring of the string-expression that represents a component of a timestamp (such as year, month, day, hour, minutes, seconds) can include fewer than the maximum number of digits for that component of the timestamp that is indicated by the corresponding format element. Any missing digits default to zero. For example, with a format-string of 'YYYY-MM-DD HH24:MI:SS', an input value of '999-3-9 5:7:2' produces the same result as '0999-03-09 05:07:02'.

Start of changeprecision-constantEnd of change
Start of changeAn integer constant that specifies the timestamp precision of the result. The value must be in the range 0 to 12. If precision-constant is not specified, the timestamp precision defaults to 6.End of change

Start of changeThe result of the function is a TIMESTAMP with a precision that is based on precision-constant.End of change

If either of the first two arguments can be null, the result can be null; if either of the first two arguments is null, the result is the null value.

The result CCSID is the appropriate CCSID of the encoding scheme of the first argument and the result subtype is the appropriate subtype of the CCSID.

Notes

Start of changeJulian and Gregorian calendar:End of change
Start of changeThe transition from the Julian calendar to the Gregorian calendar on 15 October 1582 is taken into account by this function.End of change
Determinism:
TIMESTAMP_FORMAT is a deterministic function. However, the following invocations of the function depend on the value of the special register CURRENT TIMESTAMP.
  • Start of changeformat-string is not a constantEnd of change
  • format-string is a constant and includes format elements that are locale sensitive
  • format-string is a constant and does not include a format element that fully defines the year (that is, Start of changeJ or End of changeYYYY). In this case the current year is used.
  • format-string is a constant and does not include a format element that fully defines the month (for example, Start of changeJ, End of changeMM, MONTH, or MON). In this case the current month is used.

These invocations, which depend on the value of a special register, cannot be used wherever special registers cannot be used.

Using the 'D', 'Y', and 'y' format elements:
DB2® for z/OS® does not support the 'DY', 'dy', and 'Dy' format elements that are supported by other platforms. If 'DY' or 'Dy' is specified in the format string, it is interpreted as the 'D' format element followed by the 'Y' or 'y' format element. This behavior might change in a future release. To ensure that a 'D' followed by 'Y' or 'y' is interpreted as two separate format elements, include a separator character after the 'D' format element.
Syntax alternatives:
TO_DATE can be specified as a synonym for TIMESTAMP_FORMAT.
Example 1:
Insert a row into the IN_TRAY table with a receiving timestamp that is equal to one second before the beginning of the year 2000 (December 31, 1999 at 23:59:59).
INSERT INTO IN_TRAY (RECEIVED)
	VALUES (TIMESTAMP_FORMAT('1999-12-31 23:59:59', 'YYYY-MM-DD HH24:MI:SS'))
Example 2:
An application receives strings of date information into a variable called INDATEVAR. This value is not strictly formatted and might include two or four digits for years, and one or two digits for months and days. Date components might be separated with minus sign (-) or forward-slash (/) characters and are expected to be in day, month, and year order. Time information consists of hours (in 24-hour format) and minutes, and is usually separated by a colon. Sample values include '15/12/98 13:48' and '9-3-2004 8:02'. Insert such values into the IN_TRAY table.
INSERT INTO IN_TRAY (RECEIVED)
	VALUES (TIMESTAMP_FORMAT(:INDATEVAR, 'DD/MM/RRRR HH24:MI'))
The use of 'RRRR' in the format allows for 2-digit and 4-digit year values and assigns the missing first two digits based on the current year. If 'YYYY' is used, input values with a 2-digit year will have leading zeros. The forward-slash separator also allows the minus sign character. Assuming a current year of 2007, resulting timestamp values from the sample values are as follows:
'15/12/98 13:48' --> 1998-12-15-13.48.00.000000
'9-3-2004 8:02' --> 2004-03-09-08.02.00.000000