TIMESTAMP

The TIMESTAMP function returns a timestamp from its argument or arguments.

Read syntax diagramSkip visual syntax diagramTIMESTAMP(expression-1 ,expression-2precision-constant )
expression-1 and expression-2

The rules for the arguments depend on whether a second argument is specified and the data type of the second argument.

  • If only expression-1 is specified:
    The argument must be an expression that returns a value of one of the following built-in data types: a date, a timestamp, a character string, or a graphic string. If expression-1 is a character or graphic string, its value must be one of the following:
    • A valid string representation of a date or a timestamp. For the valid formats of string representations of dates and timestamps, see String representations of datetime values.
    • A character string with an actual length of 13 that is assumed to be a result from a GENERATE_UNIQUE function. For information on GENERATE_UNIQUE, see GENERATE_UNIQUE.
  • If both arguments are specified:
    • If the second argument is expression-2:

      The first argument must be an expression that returns a value of one of the following built-in data types: a date, a character string, or a graphic string. If expression-1 is a character or graphic string, its value must be a valid string representation of a date.

      expression-2 must be an expression that returns a value of one of the following built-in data types: a time, a character string, or a graphic string. If expression-2 is a character or graphic string, its value must be a valid string representation of a time. For the valid formats of string representations of dates and times, see String representations of datetime values.

    • If the second argument is precision-constant:
      The first argument must be an expression that returns a value of one of the following built-in data types: a date, a timestamp, a character string, or a graphic string. If expression-1 is a character or graphic string, its value must be one of the following:
      • A valid string representation of a date or timestamp. For the valid formats of string representations of dates and timestamps, see String representations of datetime values.
      • A character string with an actual length of 13 that is assumed to be a result from a GENERATE_UNIQUE function. For information on GENERATE_UNIQUE, see GENERATE_UNIQUE.
precision-constant
An integer constant representing the number of fractional seconds. The value must be in the range 0 through 12.

The result of the function is a timestamp. If either argument can be null, the result can be null; if either argument is null, the result is the null value.

The other rules depend on whether the second argument is specified:

  • If both arguments are specified and the second argument is expression-2:

    The result is a TIMESTAMP(6) with the date specified by the first argument and the time specified by the second argument. The fractional seconds part of the timestamp is zero.

  • If both arguments are specified and the second argument is precision-constant:

    The result is a timestamp with the precision specified by the second argument.

  • If only one argument is specified and it is a TIMESTAMP(p):

    The result is a TIMESTAMP(p).

  • If only one argument is specified and it is a DATE:

    The result is that date with an assumed time of midnight cast to TIMESTAMP(0).

  • If only one argument is specified and it is a string:

    The result is the TIMESTAMP(6) represented by that string. If the argument is a string of length 14, the timestamp has a fractional seconds part of zero.

Note

Syntax alternatives: The CAST specification should be used to increase the portability of applications when only one argument is specified. For more information, see CAST specification.

Examples

  • Assume the following date and time values:
      SELECT TIMESTAMP( DATE('1988-12-25'), TIME('17.12.30') )
        FROM SYSIBM.SYSDUMMY1
    Returns the value '1988-12-25-17.12.30.000000'.
  • Convert a timestamp string with 7 digits of fractional seconds to a TIMESTAMP(9) value.
    TIMESTAMP('2007-09-24-15.53.37.2162474', 9)
    Returns the value ’2007-09-24-15.53.37.216247400’.