PostgreSQL MAKE_TIMESTAMP Date/Time Function



PostgreSQL MAKE_TIMESTAMP Date/Time Function

The PostgreSQL MAKE_TIMESTAMP function is used to create a timestamp value from specified year, month, day, hour, minute, and seconds fields. This function is essential for constructing timestamp values from individual components, which can be useful in various date and time-based calculations and data manipulations.


Syntax

MAKE_TIMESTAMP(year int, month int, day int, hour int, min int, sec double precision)

The MAKE_TIMESTAMP function has the following components:

  • year: The year component of the timestamp (negative years signify BC).
  • month: The month component of the timestamp.
  • day: The day component of the timestamp.
  • hour: The hour component of the timestamp.
  • min: The minute component of the timestamp.
  • sec: The seconds component of the timestamp.

Example PostgreSQL MAKE_TIMESTAMP Queries

Let's look at some examples of PostgreSQL MAKE_TIMESTAMP function queries:

1. Basic MAKE_TIMESTAMP Example

SELECT MAKE_TIMESTAMP(2013, 7, 15, 8, 15, 23.5) AS constructed_timestamp;

This query creates a timestamp from the year 2013, month 7, day 15, hour 8, minute 15, and seconds 23.5, resulting in '2013-07-15 08:15:23.5'.

2. MAKE_TIMESTAMP with BC Year

SELECT MAKE_TIMESTAMP(-44, 3, 15, 12, 0, 0) AS constructed_timestamp;

This query creates a timestamp from the year 44 BC, month 3, day 15, hour 12, minute 0, and seconds 0, resulting in '0044-03-15 12:00:00 BC'.

3. MAKE_TIMESTAMP with Column Values

SELECT id, name, MAKE_TIMESTAMP(year, month, day, hour, minute, second) AS event_timestamp
FROM events;

This query retrieves the id, name, and constructs a timestamp from the year, month, day, hour, minute, and second columns for each row in the events table.


Full Example

Let's go through a complete example that includes creating a table, inserting data, and using the MAKE_TIMESTAMP function to construct timestamp values from individual components.

Step 1: Creating a Table

This step involves creating a new table named events to store event data, including their timestamp components.

CREATE TABLE events (
    id SERIAL PRIMARY KEY,
    name TEXT,
    year INT,
    month INT,
    day INT,
    hour INT,
    minute INT,
    second DOUBLE PRECISION
);

In this example, we create a table named events with columns for id, name, year, month, day, hour, minute, and second.

Step 2: Inserting Data into the Table

This step involves inserting some sample data into the events table.

INSERT INTO events (name, year, month, day, hour, minute, second)
VALUES ('Meeting', 2021, 10, 31, 9, 30, 0),
       ('Conference', 2022, 5, 15, 14, 0, 0),
       ('Webinar', 2023, 8, 20, 11, 45, 30.75);

Here, we insert data into the events table.

Step 3: Using the MAKE_TIMESTAMP Function

This step involves using the MAKE_TIMESTAMP() function to construct timestamp values from the individual components in the events table.

Construct event timestamp from year, month, day, hour, minute, and second columns:

SELECT id, name, MAKE_TIMESTAMP(year, month, day, hour, minute, second) AS event_timestamp
FROM events;

This query constructs the event timestamp from the year, month, day, hour, minute, and second columns for each row in the events table.


Conclusion

The PostgreSQL MAKE_TIMESTAMP() function is a fundamental tool for constructing timestamp values from individual year, month, day, hour, minute, and second components. Understanding how to use the MAKE_TIMESTAMP() function and its syntax is essential for accurate timestamp construction in PostgreSQL databases.