A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql/postgresql-interval-data-type/ below:

PostgreSQL - Interval Data Type

PostgreSQL - Interval Data Type

Last Updated : 15 Jul, 2025

The interval data type in PostgreSQL stores time periods using 16 bytes of storage and supports a range from -178,000,000 years to 178,000,000 years. It provides a precision attribute ('p') that allows you to specify the number of fractional digits retained in the seconds field, enhancing the precision of time calculations and results. 

Let us get a better understanding of the Interval Data Type in PostgreSQL from this article.

Syntax
interval [ Data_fields ] [ (p) ]
Parameters: Storage Details

PostgreSQL internally stores interval values as a combination of months, days, and seconds, where:

PostgreSQL Interval Data Type Examples

Now let's jump into a few examples for demonstration. 

Example 1: Calculating Time in the Past

In this example we will query to know the time of 4 hours 25 minutes ago at the current time of last year using the below commands.

Query:

SELECT now(),
    now() - INTERVAL '1 year 4 hours 25 minutes' AS "4 hours 25 minutes ago of last year";

Output: 


Example 2: Formatting Interval Values

In this example, we will convert an interval value ta string format using the 'TO_CHAR()' function. The 'TO_CHAR()' function takes the first argument as an interval value, the second one as the format, and returns a string that represents the interval in the specified format. 

Query:

SELECT 
    TO_CHAR(
        INTERVAL '15h 25m 12s',
        'HH24:MI:SS'
    );

Output: 

Important Points About PostgreSQL Interval Data Type


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4