Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Query Date and Time in PostgreSQL
PostgreSQL provides a wide range of functions and operators to handle date and time data types efficiently. This article explores how to query date and time values, perform comparisons, and manipulate data using SQL functions and operators in PostgreSQL.
1. Querying Date and Time Values
In PostgreSQL, the DATE
type stores the date (year, month, and day), while the TIME
type stores the time of day without any time zone. The TIMESTAMP
type combines both date and time information.
SELECT * FROM events WHERE event_date = '2025-05-01';
This query fetches records from the events
table where the event_date
is '2025-05-01'.
2. Working with Date and Time Functions
PostgreSQL has built-in functions to manipulate date and time data. Some of the most commonly used functions are:
CURRENT_DATE
: Returns the current date.CURRENT_TIME
: Returns the current time.CURRENT_TIMESTAMP
: Returns the current date and time.DATE_PART
: Extracts a specific part of the date (e.g., year, month, day).
SELECT CURRENT_TIMESTAMP;
This query returns the current date and time.
3. Date and Time Comparisons
You can compare dates and times using standard comparison operators like =
, !=
, >
, and <
. Here’s an example of filtering records that occurred after a specific date:
SELECT * FROM meetings WHERE meeting_date > '2025-01-01';
This query retrieves records where the meeting_date
is after January 1st, 2025.
4. Timezone Handling
PostgreSQL supports time zone-aware date and time types. The TIMESTAMP WITH TIME ZONE
type allows storing both date, time, and timezone information. You can convert time zones with the AT TIME ZONE
operator.
SELECT event_date AT TIME ZONE 'UTC' FROM events;
This query converts the event_date
to UTC time zone.
5. Extracting Parts of a Date or Time
If you need to extract specific parts of a date or time, PostgreSQL’s DATE_PART
function is extremely useful:
SELECT DATE_PART('year', CURRENT_DATE);
This query returns the current year.