Introduction
Dates
Time
Timestamps
Extract functions
Timezone conversion
Intervals
Current date and time
37. Current date & time with intervals
Revision

Instruction

Alright. How can we use these functions in our queries? We can use them to find facts which took place in the last week, last month etc. Take a look:

SELECT id
FROM flight
WHERE date > CURRENT_DATE - INTERVAL '7' DAY;

The above query will find all flights which took place in the last 7 days.

We use the structure INTERVAL 'x' UNIT where x is the number and UNIT is SECOND, MINUTE, HOUR, DAY, MONTH or YEAR. Our database takes the current date and puts it back 7 days.

Exercise

Find the id of all the aircraft which were produced earlier than 3 months ago.

Stuck? Here's a hint!

Use CURRENT_DATE - INTERVAL '3' MONTH to get the date 3 months ago.

Console

Code editor

Result

TableConsole