Date and Time Calculations

To facilitate the calculation of dates and times, a series of functions are available that can use values of these data types to perform calculations.

Prerequisites

You require the demo data for the SQL Tutorial.

Start the query tool SQL Studio as database administrator MONA with password RED and log on to the demo database instance DEMODB.

Examples

SELECT arrival, ADDDATE (arrival,2) new_arrival, rno
  FROM hotel.reservation
    WHERE rno = 130

Postponing the reservation date by two days

Result

ARRIVAL

NEW_ARRIVAL

RNO

2005-02-01

2005-02-03

130

SELECT arrival, departure, DATEDIFF(departure, arrival) overnight_stays, rno
  FROM hotel.reservation
    WHERE rno = 130

Number of overnight stays

Result

ARRIVAL

DEPARTURE

OVERNIGHT_STAYS

RNO

2005-02-01

2005-02-03

2

130

See also:

SQL Reference Manual, Date Function (date_function), Time Function (time_function), Extraction (extraction_function)

A variety of date formats (EUR, INTERNAL, ISO, JIS, and USA) are available for processing date values.

See also:

SQL Reference Manual, Date and Time Format (datetimeformat)

More examples for Data Query