SECOND scalar function

The SECOND function returns the seconds part of a value with optional fractional seconds.

Read syntax diagramSkip visual syntax diagramSECOND(expression ,integer-constant )

The schema is SYSIBM.

expression
An expression that returns a value of one of the following built-in data types: DATE, TIME, TIMESTAMP, time duration, timestamp duration, or a valid character string representation of a date, time, or timestamp that is not a CLOB. If expression is a DATE or a valid string representation of a date, it is first converted to a TIMESTAMP(0) value, assuming a time of exactly midnight (00.00.00). If expression is a valid string representation of a timestamp, it is first converted to a TIMESTAMP(12) value. In a Unicode database, if a supplied argument is a graphic string, it is first converted to a character string before the function is executed.
integer-constant
An integer constant representing the scale for the fractional seconds. The value must be in the range 0 through 12.

The result of the function with a single argument is a large integer. The result of the function with two arguments is DECIMAL(2+s,s) where s is the value of the integer-constant. If the first argument can be null, the result can be null; if the first argument is null, the result is the null value.

The other rules depend on the data type of the first argument and the number of arguments:
  • If the first argument is a DATE, TIME, TIMESTAMP, or valid string representation of a date, time, or timestamp:
    • If only one argument is specified, the result is the seconds part of the value (0 and 59).
    • If both arguments are specified, the result is the seconds part of the value (0 to 59) and integer-constant digits of the fractional seconds part of the value where applicable. If there are no fractional seconds in the value, then zeros are returned.
  • If the first argument is a time duration or timestamp duration:
    • If only one argument is specified, the result is the seconds part of the value (-99 to 99).
    • If both arguments are specified, the result is the seconds part of the value (-99 to 99) and integer-constant digits of the fractional seconds part of the value where applicable. If there are no fractional seconds in the value then zeros are returned. A nonzero result has the same sign as the argument.

Examples

  • Example 1: Assume that the host variable TIME_DUR (decimal(6,0)) has the value 153045.
      SELECT SECOND(:TIME_DUR)
        FROM SYSIBM.SYSDUMMY1

    Returns the value 45.

  • Example 2: Assume that the column RECEIVED (whose data type is TIMESTAMP) has an internal value equivalent to 1988-12-25-17.12.30.000000.
      SELECT SECOND(RECEIVED)
        FROM IN_TRAY

    Returns the value 30.

  • Example 3: Get the seconds with fractional seconds from a current timestamp with milliseconds.
      SELECT SECOND (CURRENTTIMESTAMP(3), 3)
        FROM SYSIBM.SYSDUMMY1

    Returns a DECIMAL(5,3) value based on the current timestamp that could be something like 54.321.