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 date
data type is used to store calendar dates, without the time of day. The date
type can store dates within a wide range (from 4713 BC to 5874897 AD), making it versatile for a variety of applications.
Here's a deeper look into the date
data type:
PostgreSQL is flexible in terms of date input formats, and it supports various formats like 'YYYY-MM-DD'
, 'MM/DD/YYYY'
, 'DD-MM-YYYY'
, and more. However, the ISO 8601 format 'YYYY-MM-DD'
is the recommended way.
INSERT INTO your_table (date_column) VALUES ('2021-09-12');
The date
type requires 4 bytes for storage.
Current Date:
SELECT CURRENT_DATE;
Date Arithmetic: You can add or subtract integers and intervals from dates. For instance:
SELECT CURRENT_DATE + 5; -- Adds 5 days to the current date
Extract Fields: To extract specific fields (e.g., year, month, day) from a date:
SELECT EXTRACT(YEAR FROM date_column) FROM your_table;
Age: To calculate the age between two dates or between a specific date and today:
SELECT AGE(date_column) FROM your_table; -- Age from the date_column to today SELECT AGE(date_column, '2021-01-01') FROM your_table; -- Age between date_column and January 1, 2021
Truncating Date:
While the date
type doesn't store time, if you're working with timestamps and want to truncate them to date:
SELECT DATE_TRUNC('day', timestamp_column) FROM your_table;
Interval Arithmetic: To add or subtract an interval (like 2 months) from a date:
SELECT date_column + INTERVAL '2 months' FROM your_table;
Date Comparisons:
You can use standard comparison operators (<
, >
, <=
, >=
, =
, <>
) to compare dates.
By default, PostgreSQL displays dates in the format 'YYYY-MM-DD'
. However, this can be modified using the DateStyle
setting.
Remember, while the date
type only represents the calendar date, if you need to represent both the date and time, you would use the timestamp
or timestamp with time zone
data types.
Storing and retrieving dates in PostgreSQL:
DATE
data type to store and retrieve date values.CREATE TABLE your_table ( id SERIAL PRIMARY KEY, event_date DATE ); INSERT INTO your_table (event_date) VALUES ('2023-01-01');
Formatting dates with TO_CHAR in PostgreSQL:
TO_CHAR
to format date values as strings.SELECT TO_CHAR(event_date, 'YYYY-MM-DD') AS formatted_date FROM your_table;
Date arithmetic with INTERVAL in PostgreSQL:
INTERVAL
keyword.SELECT event_date + INTERVAL '1 month' AS future_date FROM your_table;
Comparing dates with operators in PostgreSQL:
SELECT * FROM your_table WHERE event_date > '2023-01-01';
Handling time zones with TIMESTAMP WITH TIME ZONE in PostgreSQL:
TIMESTAMP WITH TIME ZONE
to handle time zones.CREATE TABLE your_table ( id SERIAL PRIMARY KEY, event_timestamp TIMESTAMP WITH TIME ZONE ); INSERT INTO your_table (event_timestamp) VALUES ('2023-01-01 12:00:00 UTC');
Extracting components (year, month, day) from dates in PostgreSQL:
SELECT EXTRACT(YEAR FROM event_date) AS year, EXTRACT(MONTH FROM event_date) AS month, EXTRACT(DAY FROM event_date) AS day FROM your_table;
Using current date and time with CURRENT_DATE and CURRENT_TIMESTAMP in PostgreSQL:
CURRENT_DATE
and CURRENT_TIMESTAMP
for the current date and time.SELECT CURRENT_DATE AS today, CURRENT_TIMESTAMP AS now;
Casting and converting date data types in PostgreSQL:
SELECT event_date, CAST(event_date AS VARCHAR) AS date_as_text, TO_DATE('2023-01-01', 'YYYY-MM-DD') AS text_to_date FROM your_table;