PostgreSQL – TIME Data Type

The time data type is used to store time of day values without any associated date information. It represents a specific time on the clock, including hours, minutes, seconds, and fractions of a second. Here are some key points about the time data type:

  1. Format: Times are stored in the format ‘HH:MI:SS’, where ‘HH’ represents the hours (00 to 23), ‘MI’ represents the minutes (00 to 59), and ‘SS’ represents the seconds (00 to 59). Fractional seconds can also be included.
  2. Precision: The time data type can store time values with fractional seconds up to microsecond precision (6 decimal places).
  3. Time Functions: PostgreSQL provides various functions for manipulating and working with time values, such as extracting components (hours, minutes, seconds), calculating differences between times, and formatting times for display.

Here’s an example of using the time data type:

CREATE TABLE appointments 
(
appointment_id serial PRIMARY KEY, 
appointment_time time, 
appointment_description varchar(200) 
); 
INSERT INTO appointments (appointment_time, appointment_description) 
VALUES ('14:30:00', 'Meeting with client'); 
SELECT * FROM appointments 
WHERE appointment_time <= '15:00:00';

In this example, an appointment_time column of type time is used to store appointment times. The time data type is suitable when you need to store and manipulate specific times of day without any date-related information.

Similar Posts