PostgreSQL - Interval Data Type Last Updated : 25 Aug, 2024 Comments Improve Suggest changes Like Article Like Report 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.Syntaxinterval [ Data_fields ] [ (p) ] Parameters:fields: Specifies which parts of the interval to store (e.g., YEAR, MONTH, DAY, HOUR, MINUTE, SECOND).p: Precision, defines the fractional seconds precision.Storage DetailsPostgreSQL internally stores interval values as a combination of months, days, and seconds, where:Months and days are stored as integers.Seconds can include fractions, based on the specified precision.PostgreSQL Interval Data Type ExamplesNow let's jump into a few examples for demonstration. Example 1: Calculating Time in the PastIn 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 ValuesIn 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 TypeUsers can specify the fractional seconds precision with the precision attribute ('p'). This allows for precise control over how much detail about seconds is stored.Combine 'interval' with other PostgreSQL functions like 'date_trunc' to simplify complex time-based queries and enhance data granularity.PostgreSQL automatically normalizes interval values when they are stored. For example, if you input an interval of '25 hours', PostgreSQL will store it as '1 day and 1 hour'.The interval type supports mixed units, like combining years, months, days, and times together. Comment More infoAdvertise with us Next Article PostgreSQL - Interval Data Type R RajuKumar19 Follow Improve Article Tags : PostgreSQL postgreSQL postgreSQL-dataTypes Similar Reads PostgreSQL - INTEGER Data Type In PostgreSQL, the INTEGER data type is widely used for storing numerical data efficiently. It is a 4-byte data type that allows us to store whole numbers within a specified range, making it ideal for various use cases like population counts, active user statistics, and more.In this article, we will 4 min read PostgreSQL - Date Data Type PostgreSQL offers powerful DATE data type and date functions to efficiently handle date and time information. PostgreSQL DATE data type allows for storing and manipulating calendar dates while its robust set of date functions enables users to perform operations like date arithmetic and formatting. I 4 min read PostgreSQL - NUMERIC Data Type In PostgreSQL, the NUMERIC data type is designed for high-precision number storage by making it ideal for financial and scientific applications where accuracy is critical. It supports a large number of digits both before and after the decimal point, minimizing rounding errors. Understanding the nuan 5 min read PostgreSQL - BIGINT Integer Data Type PostgreSQL is a powerful open-source relational database management system that offers various integer data types to suit different needs. One of these is the BIGINT data type, designed for storing very large integers. In this article, we will explain the details of the BIGINT data type in PostgreSQ 3 min read PostgreSQL - Data Types PostgreSQL is a powerful, open-source relational database management system that supports a wide variety of data types. These data types are essential for defining the nature of the data stored in a database column. which allows developers to define, store, and manipulate data in a way that aligns w 5 min read PostgreSQL - SMALLINT Integer Data Type In PostgreSQL, the SMALLINT data type is a compact, efficient way to store integer values within a small range. Using only 2 bytes of storage, SMALLINT is ideal for scenarios where the range of possible values is relatively small, such as the age of individuals or the number of pages in a book.In th 4 min read PostgreSQL - Array Data Type PostgreSQL provides an advanced and flexible feature known as the Array Data Type, which allows us to store multiple values in a single column. Arrays in PostgreSQL can be used with all built-in data types and even user-defined types, enabling a wide range of use cases. In this article, we will expl 4 min read PostgreSQL - TIME Data Type In PostgreSQL, the TIME data type is essential for applications that require precise time tracking, such as scheduling systems and event logging. This data type allows for accurate time-based entries without storing date information. PostgreSQLâs TIME data type also supports fractional seconds for u 4 min read PostgreSQL - CHAR Data Type The CHAR data type in PostgreSQL is one of the essential character data types for storing fixed-length strings. Unlike VARCHAR, which stores variable-length data, CHAR is used when we need to store a fixed-length string.This article will explain the CHAR data type in PostgreSQL, its syntax, common u 5 min read PostgreSQL - TEXT Data Type PostgreSQL provides a highly flexible character data type known as TEXT, designed to store character strings of virtually unlimited length. Unlike the VARCHAR data type, which can be limited to a specified length, the TEXT data type offers the same efficiency and performance without the length const 3 min read Like