Start of change

DAYS_BETWEEN

The DAYS_BETWEEN function returns the number of full days between the specified arguments.

FL 507

Start of change
Passthrough-only expression: This function is passthrough-only and cannot run on Db2 for z/OS® without acceleration. For information about invoking this function, see Accelerating queries with passthrough-only expressions.
End of change
Read syntax diagramSkip visual syntax diagramDAYS_BETWEEN(expression1 ,expression2)

The schema is SYSIBM.

expression1
An expression that specifies the first datetime value to compute the number of full days between two datetime values. The expression must return a value that is a DATE, TIMESTAMP WITHOUT TIME ZONE, CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type. CHAR, VARCHAR, GRAPHIC, and VARGRAPHIC are supported by using implicit casting. If expression1 is a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type, it must be a valid string that is accepted by the TIMESTAMP scalar function and does not contain a time zone.
expression2
An expression that specifies the second datetime value to compute the number of full days between two datetime values. The expression must return a value that is a DATE, TIMESTAMP WITHOUT TIME ZONE, CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type. CHAR, VARCHAR, GRAPHIC, and VARGRAPHIC are supported by using implicit casting. If expression2 is a CHAR, VARCHAR, GRAPHIC, or VARGRAPHIC data type, it must be a valid string that is accepted by the TIMESTAMP scalar function and does not contain a time zone.

If there is less than a full day between expression1 and expression2, the result is zero. If expression1 is later than expression2, the result is positive. If expression1 is earlier than expression2, the result is negative. If expression1 or expression2 contains time information, this information is also used to determine the number of full days. If expression1 or expression2 does not contain time information, a time of midnight (00.00.00) is used for the argument that is missing time information.

The result of the function is an INTEGER. If either argument can be null, the result can be null. If either argument is null, the result is the null value.

Examples

  • Set the host variable NUM_DAYS with the number of full days between 2012-03-03 and 2012-02-28.
       SET :NUM_DAYS = DAYS_BETWEEN(DATE '2012-03-03',
          DATE '2012-02-28')
    The host variable NUM_DAYS is set to 4 because an additional day is incurred for February 29, 2012.
  • Set the host variable NUM_DAYS with the number of full days between 2013-09-11-23.59.59 and 2013-09-01-00.00.00.
       SET :NUM_DAYS = DAYS_BETWEEN(TIMESTAMP '2013-09-11-23.59.59',
          TIMESTAMP '2013-09-01-00.00.00')
    The host variable NUM_DAYS is set to 10 because there is 1 second less than a full 11 days between the arguments. It is positive because the first argument is later than the second argument.
  • Set the host variable NUM_DAYS with the number of full days between 2013-09-01-00.00.00 and 2013-09-11-23.59.59.
       SET :NUM_DAYS = DAYS_BETWEEN(TIMESTAMP '2013-09-01-00.00.00',
          TIMESTAMP '2013-09-11-23.59.59')
    The host variable NUM_DAYS is set to -10 because there is 1 second less than a full 11 days between the arguments. It is negative because the first argument is earlier than the second argument.
End of change