PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the INTERVAL
data type is used to represent and store a time span. This could be a number of years, months, days, hours, minutes, seconds, or a combination of these. It's a versatile type that allows you to work with time durations.
Syntax: The general syntax for the INTERVAL
type is:
INTERVAL [ fields ] [ (p) ]
fields
can be one of:YEAR
MONTH
DAY
HOUR
MINUTE
SECOND
YEAR TO MONTH
, DAY TO SECOND
, etc.p
is the precision for the SECOND
field, determining the number of fractional digits retained in the seconds field.Examples:
Here are some ways you might define or use the INTERVAL
type:
A simple interval of 3 days:
SELECT INTERVAL '3 days';
An interval combining multiple fields:
SELECT INTERVAL '2 years 3 months 4 days 5 hours';
Specifying precision:
SELECT INTERVAL '1.234567 second';
Operations:
INTERVAL
can be used with datetime arithmetic. For instance, adding an interval to a timestamp:
SELECT NOW() + INTERVAL '1 year 2 days';
Extracting components:
You can extract specific components from an INTERVAL
using the EXTRACT
function:
SELECT EXTRACT(DAY FROM INTERVAL '5 days 4 hours');
This will return 5
since we're extracting the day component.
Casting:
You can cast a string to an interval:
SELECT '100 days'::INTERVAL;
Storage:
The storage requirement for an INTERVAL
value depends on the specified or implied fields. It could be from 4 to 12 bytes.
Limitations:
The largest possible interval that can be represented is approximately 178 million years.
Be aware that monthly intervals are treated separately from daily intervals, due to the varying number of days in different months. For example, 1 month
is not equivalent to 30 days
or 31 days
, but is an interval representing a single month regardless of the number of days.
The INTERVAL
type in PostgreSQL is a powerful way to work with and represent durations or time spans. Combined with date/time types, it provides comprehensive functionality for date and time arithmetic and manipulations.
How to use INTERVAL data type in PostgreSQL:
INTERVAL
data type represents a period of time.SELECT INTERVAL '1 day' + INTERVAL '3 hours';
Defining INTERVAL columns in PostgreSQL tables:
INTERVAL
column when creating a table.CREATE TABLE example_table ( interval_column INTERVAL );
Arithmetic operations with INTERVAL in PostgreSQL:
INTERVAL
.SELECT '2023-01-01'::DATE + INTERVAL '1 month';
Casting and converting data to INTERVAL in PostgreSQL:
INTERVAL
.SELECT '3 days'::INTERVAL;
Working with INTERVAL literals in PostgreSQL:
SELECT INTERVAL '1 hour 30 minutes';
Using INTERVAL with date and time values in PostgreSQL:
INTERVAL
with date and time values.SELECT CURRENT_TIMESTAMP + INTERVAL '5 days';
Indexing on INTERVAL columns in PostgreSQL:
INTERVAL
columns for better performance.CREATE INDEX interval_index ON example_table (interval_column);
Handling NULL values with INTERVAL in PostgreSQL:
NULL
values for INTERVAL
columns.CREATE TABLE null_interval_table ( interval_column INTERVAL NULL );
Extracting components from INTERVAL in PostgreSQL:
INTERVAL
.SELECT EXTRACT(DAY FROM INTERVAL '3 days');
Creating and manipulating intervals in PostgreSQL:
SELECT INTERVAL '1 day' * 3;
Calculating age using INTERVAL in PostgreSQL:
AGE
function.SELECT AGE('2000-01-01'::DATE, '1970-01-01'::DATE);
Aggregating data with INTERVAL in PostgreSQL:
INTERVAL
.SELECT SUM(interval_column) FROM example_table;
Using INTERVAL in WHERE and HAVING clauses in PostgreSQL:
INTERVAL
in conditions for filtering data.SELECT * FROM example_table WHERE interval_column > INTERVAL '1 day';
Formatting INTERVAL values in PostgreSQL:
INTERVAL
values for display.SELECT TO_CHAR(INTERVAL '3 days', 'DD HH24:MI:SS');
Comparing INTERVAL with other date/time data types in PostgreSQL:
INTERVAL
with other date/time types to avoid unexpected behavior.SELECT '2023-01-01'::DATE + INTERVAL '1 day' = '2023-01-02'::DATE;
Using INTERVAL in date arithmetic in PostgreSQL:
INTERVAL
in date arithmetic for dynamic calculations.SELECT CURRENT_DATE + INTERVAL '7 days';
INTERVAL and foreign key relationships in PostgreSQL:
INTERVAL
columns.CREATE TABLE parent_table ( id SERIAL PRIMARY KEY ); CREATE TABLE child_table ( id SERIAL PRIMARY KEY, parent_id INTEGER REFERENCES parent_table(id), interval_column INTERVAL );