Updated May 15, 2023
Introduction to PostgreSQL TIME
PostgreSQL provides various data types to handle the data. The TIME data type will allow the user to store the values like the time of the day. The time range for the day starts from 00:00:00 to 24:00:00. We can store the time of day in various formats like HH: MM, HH:MM:: SS, and HHMMSS, etc. Also, we can use precision as well by using formats like MM: SS.pppppp, HH:MM: SS.pppppp, and HHMMSS.pppppp, etc., which can be up to six digits. The PostgreSQL TIME data type requires 8 bytes to store the time values.
Syntax:
Given below is the following syntax to illustrate the declaration of a column having as data type as the TIME data type:
column TIME(precision);
Explanation:
- column: The column name which will have a data type as TIME.
- precision: This is a fractional digits number placed in the seconds’ field. This can be up to six digits.
How does PostgreSQL TIME Data Type work?
Including the ISO 8601 and the SQL-compatibility, the PostgreSQL also allows all TIME formats.
Consider the following TIME formats allowed in PostgreSQL.
- HH:MM
- HH:MM:SS
- HHMMSS
Now, let us see the following values as per the above TIME formats.
- 03:04
- 03:04:05
- 030405
We can include the precision value in the TIME value using the following TIME formats.
- MM: SS.pppppp
- HH:MM:SS.pppppp
- pppppp
The character p is used for defining precision.
Now, let us see the following values as per the above TIME formats.
- 02:36.666666
- 03:04:05.444444
- 030506.999999
Examples of PostgreSQL TIME
Given below are the examples mentioned:
Example #1
We generally store the time of the day with the help of the data type named TIME, like the tuition or event.
Here we will create a new table of name ‘tuitions’ using the CREATE TABLE statement, which will store the details of the ‘tuitions’ information.
Code:
CREATE TABLE tuitions (
id serial PRIMARY KEY,
tuition_name VARCHAR NOT NULL,
tuition_start_tm TIME NOT NULL,
tuition_end_tm TIME NOT NULL
);
We will insert some data into the tuitions table using the INSERT INTO statement as follows.
Code:
INSERT INTO tuitions(tuition_name, tuition_start_tm, tuition_end_tm)
VALUES
('Maths', '07:00:00', '08:45:00'),
('English', '09:00:00', '11:45:00'),
('Algebra', '12:15:00', '14:00:00'),
('CS', '14:15:00', '17:00:00'),
('History', '17:15:00', '19:00:00');
Illustrate the result of the tuitions table by using the following SQL statement and a snapshot.
Code:
SELECT * FROM tuitions;
Output:
Example #2
Consider the following examples of Handling PostgreSQL TIME values.
a. Get the current time
We can use the CURRENT_TIME function to get the current time with the time zone as follows.
Code:
SELECT CURRENT_TIME;
Output:
b. We can use the CURRENT_TIME (precision) function to get the current time’s value containing the particular precision.
Code:
SELECT CURRENT_TIME(6);
Output:
If we have not defined the precision, then we get the full precision as a result of the CURRENT_TIME function.
Example #3
To get the local time, we can use the PostgreSQL LOCALTIME function.
Code:
SELECT LOCALTIME;
Output:
Example #4
We can use the PostgreSQL LOCAL TIME (precision) function as defined below to get the local time’s value containing the particular precision.
Code:
SELECT localtime(2);
Output:
Example #5
We can extract fields like seconds, minutes, and hours from a value formatted as TIME using the PostgreSQL EXTRACT function.
Code:
EXTRACT(extract_field FROM time);
Explanation:
- extract_field: This value can be the milliseconds, second, minute, hour, etc.
Code:
SELECT
CURRENT_TIME,
EXTRACT
( HOUR FROM CURRENT_TIME) as CURRENT_TIME_HOUR,
EXTRACT
( MINUTE FROM CURRENT_TIME) as CURRENT_TIME_MINUTE,
EXTRACT
( SECOND FROM CURRENT_TIME) as CURRENT_TIME_SECOND,
EXTRACT
( milliseconds FROM CURRENT_TIME) as CURRENT_TIME_MILLISECONDS;
Output:
Example #6
We can convert the time from one time zone to another time zone.
Syntax:
[time_zone TIME] AT TIME ZONE time_zone
Consider the following example, which will convert the CURRENT_TIME to the time zone GMT+2.
Code:
SELECT CURRENT_TIME AT TIME ZONE 'GMT+2';
Output:
Example #7
We can perform some arithmetic operations like addition, subtraction, and multiplication on time values.
a. Consider the following example to get the interval.
Code:
SELECT
time '11:00' - time '03:00';
Output:
b. Consider another example where the current time incremented by 3 hours.
Code:
SELECT CURRENT_TIME + interval '3 hours';
Output:
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL TIME” was beneficial to you. You can view EDUCBA’s recommended articles for more information.