PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - Date Data Type

In PostgreSQL, the date data type is used to store calendar dates, without the time of day. The date type can store dates within a wide range (from 4713 BC to 5874897 AD), making it versatile for a variety of applications.

Here's a deeper look into the date data type:

Input Format:

PostgreSQL is flexible in terms of date input formats, and it supports various formats like 'YYYY-MM-DD', 'MM/DD/YYYY', 'DD-MM-YYYY', and more. However, the ISO 8601 format 'YYYY-MM-DD' is the recommended way.

INSERT INTO your_table (date_column) VALUES ('2021-09-12');

Storage Size:

The date type requires 4 bytes for storage.

Range:

  • Minimum: 4713 BC
  • Maximum: 5874897 AD

Functions and Operations:

  1. Current Date:

    SELECT CURRENT_DATE;
    
  2. Date Arithmetic: You can add or subtract integers and intervals from dates. For instance:

    SELECT CURRENT_DATE + 5; -- Adds 5 days to the current date
    
  3. Extract Fields: To extract specific fields (e.g., year, month, day) from a date:

    SELECT EXTRACT(YEAR FROM date_column) FROM your_table;
    
  4. Age: To calculate the age between two dates or between a specific date and today:

    SELECT AGE(date_column) FROM your_table;  -- Age from the date_column to today
    SELECT AGE(date_column, '2021-01-01') FROM your_table;  -- Age between date_column and January 1, 2021
    
  5. Truncating Date: While the date type doesn't store time, if you're working with timestamps and want to truncate them to date:

    SELECT DATE_TRUNC('day', timestamp_column) FROM your_table;
    
  6. Interval Arithmetic: To add or subtract an interval (like 2 months) from a date:

    SELECT date_column + INTERVAL '2 months' FROM your_table;
    
  7. Date Comparisons: You can use standard comparison operators (<, >, <=, >=, =, <>) to compare dates.

Output Format:

By default, PostgreSQL displays dates in the format 'YYYY-MM-DD'. However, this can be modified using the DateStyle setting.

Remember, while the date type only represents the calendar date, if you need to represent both the date and time, you would use the timestamp or timestamp with time zone data types.

  1. Storing and retrieving dates in PostgreSQL:

    • Description: Use the DATE data type to store and retrieve date values.
    • Code:
      CREATE TABLE your_table (
          id SERIAL PRIMARY KEY,
          event_date DATE
      );
      
      INSERT INTO your_table (event_date) VALUES ('2023-01-01');
      
  2. Formatting dates with TO_CHAR in PostgreSQL:

    • Description: Use TO_CHAR to format date values as strings.
    • Code:
      SELECT TO_CHAR(event_date, 'YYYY-MM-DD') AS formatted_date
      FROM your_table;
      
  3. Date arithmetic with INTERVAL in PostgreSQL:

    • Description: Perform date arithmetic using the INTERVAL keyword.
    • Code:
      SELECT event_date + INTERVAL '1 month' AS future_date
      FROM your_table;
      
  4. Comparing dates with operators in PostgreSQL:

    • Description: Use comparison operators to compare dates.
    • Code:
      SELECT *
      FROM your_table
      WHERE event_date > '2023-01-01';
      
  5. Handling time zones with TIMESTAMP WITH TIME ZONE in PostgreSQL:

    • Description: Use TIMESTAMP WITH TIME ZONE to handle time zones.
    • Code:
      CREATE TABLE your_table (
          id SERIAL PRIMARY KEY,
          event_timestamp TIMESTAMP WITH TIME ZONE
      );
      
      INSERT INTO your_table (event_timestamp) VALUES ('2023-01-01 12:00:00 UTC');
      
  6. Extracting components (year, month, day) from dates in PostgreSQL:

    • Description: Extract specific components from date values.
    • Code:
      SELECT
          EXTRACT(YEAR FROM event_date) AS year,
          EXTRACT(MONTH FROM event_date) AS month,
          EXTRACT(DAY FROM event_date) AS day
      FROM your_table;
      
  7. Using current date and time with CURRENT_DATE and CURRENT_TIMESTAMP in PostgreSQL:

    • Description: Utilize CURRENT_DATE and CURRENT_TIMESTAMP for the current date and time.
    • Code:
      SELECT CURRENT_DATE AS today, CURRENT_TIMESTAMP AS now;
      
  8. Casting and converting date data types in PostgreSQL:

    • Description: Cast or convert date values to different data types.
    • Code:
      SELECT
          event_date,
          CAST(event_date AS VARCHAR) AS date_as_text,
          TO_DATE('2023-01-01', 'YYYY-MM-DD') AS text_to_date
      FROM your_table;