SQL - Scalar functions - TIMESTAMP_FORMAT
The TIMESTAMP_FORMAT function returns a timestamp that is based on the interpretation of the input string using the specified format.
The schema is SYSIBM.
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 Table 1 (SQLSTATE 22007). For example, format-string cannot contain both YY and YYYY, because they are both used to interpret the year component of string-expression. Refer to the table to determine which format elements cannot be specified together. Two format elements can optionally be separated by one or more of the following separator characters:
The DAY, Day, day, DY, Dy, dy, and D format elements do not contribute to any components of the resulting timestamp. However, a specified value for any of these format elements must be correct for the combination of the year, month, and day components of the resulting timestamp (SQLSTATE 22007). For example, assuming a value of 'en_US' is used for locale-name, a value of 'Monday 2008-10-06' for string-expression is valid for a value of 'Day YYYY-MM-DD'. However, value of 'Tuesday 2008-10-06' for string-expression would result in error for the same format-string.
The RR and RRRR format elements can be used to alter how a specification for a year is to be interpreted by adjusting the value to produce a 2-digit value or a 4-digit value depending on the leftmost two digits of the current year according to the following table.
The following defaults are used when a format-string does not include a format element for one of the following components of a timestamp:
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 representing a component of a timestamp (such as year, month, day, hour, minutes, seconds) can include less than the maximum number of digits for that component of the timestamp 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' would produce the same result as '0999-03-09 05:07:02'.
If format-string is not specified, string-expression will be interpreted using a default format based on the value of the special register CURRENT LOCALE LC_TIME.
If locale-name is not specified, the value of the special register CURRENT LOCALE LC_TIME is used.
The result of the function is a TIMESTAMP with a precision based on precision-constant. 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.
INSERT INTO IN_TRAY (RECEIVED) VALUES (TIMESTAMP_FORMAT('1999-12-31 23:59:59', 'YYYY-MM-DD HH24:MI:SS'))
INSERT INTO IN_TRAY (RECEIVED) VALUES (TIMESTAMP_FORMAT(:INDATEVAR, 'DD/MM/RRRR HH24:MI'))
'15/12/98 13:48' --> 1998-12-15-13.48.00.000000 '9-3-2004 8:02' --> 2004-03-09-08.02.00.000000