Best April deals - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Dealing with dates
Working with time
Date and time data types in PostgreSQL
Extracting dates and times
Intervals
Timezone conversion
Format date and time
Current date and time data
Summary

Instruction

Okay! Let's use a few INTERVALs right away:

SELECT
  withdrawn_timestamp,
  withdrawn_timestamp
    + INTERVAL '1 year'
    - INTERVAL '3 month'
    AS new_launched_timestamp
FROM aircraft
WHERE id = 3;

The above query will add one year minus three months to the timestamp stored in the withdrawn_timestamp column. (Yes, you may simply add 9 months, this is just an example 😉)

You can also create an interval with multiple fields, using this syntax:

  • INTERVAL '1 year 2 months 3 days'
  • INTERVAL '2 weeks ago'

For example:

SELECT
  withdrawn_timestamp,
  withdrawn_timestamp
    + INTERVAL '1 year 3 months'
    AS new_launched_timestamp
FROM aircraft
WHERE id = 3;

Exercise

PerfectAir, due to unexpected work on the runway of Frankfurt airport (the FRA code). needs to reschedule all departing flights... Show the code, new departure and arrival times forwarded by 1 hour and 15 minutes (show these as new_departure_time and new_arrival_time columns).

Stuck? Here's a hint!

Use this expression:

+ INTERVAL '1 hour 15 minutes'