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

Start of changeThe rules for the arguments depend on whether a second argument is specified and the data type of the second argument.End of change

  • 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: Start of changea date,End of change 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 Start of changea date orEnd of change a timestamp. For the valid formats of string representations of Start of changedates andEnd of change 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.

    • Start of changeIf 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 Start of changedate orEnd of change 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.
      End of change
Start of changeprecision-constantEnd of change
Start of changeAn integer constant representing the number of fractional seconds. The value must be in the range 0 through 12.End of change

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 Start of changeand the second argument is expression-2End of change:

    The result is a Start of changeTIMESTAMP(6)End of change with the date specified by the first argument and the time specified by the second argument. The Start of changefractional secondsEnd of change part of the timestamp is zero.

  • Start of changeIf both arguments are specified and the second argument is precision-constant:

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

    End of change
  • If only one argument is specified and it is a Start of changeTIMESTAMP(pEnd of change):

    The result is a Start of changeTIMESTAMP(p)End of change.

  • Start of changeIf 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).

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

    The result is the Start of changeTIMESTAMP(6)End of change 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'.
  • Start of changeConvert 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’.End of change