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 - CURRENT_DATE Function

In PostgreSQL, the CURRENT_DATE function returns the current date. It's a special date/time function that returns the current date as a value in 'YYYY-MM-DD' format or YYYYMMDD format depending on the usage context.

Here's how you can use it:

1. To get the current date:

SELECT CURRENT_DATE;

2. Using it in a WHERE clause:

Imagine you have a table orders with a date column order_date. If you want to fetch all orders placed today, you can use:

SELECT * 
FROM orders 
WHERE order_date = CURRENT_DATE;

3. Using it in combination with interval:

To get the date 7 days ago:

SELECT CURRENT_DATE - INTERVAL '7 days';

Or to get the date 5 days from now:

SELECT CURRENT_DATE + INTERVAL '5 days';

Remember, CURRENT_DATE returns just the date without time. If you need the current timestamp including the date and time, you would use CURRENT_TIMESTAMP or now().

  1. Retrieving the current date with CURRENT_DATE in PostgreSQL:

    • Description: Retrieves the current date.
    • Code:
      SELECT CURRENT_DATE;
      
  2. Using CURRENT_DATE in WHERE clauses in PostgreSQL:

    • Description: Filters records based on the current date.
    • Code:
      SELECT *
      FROM your_table
      WHERE date_column = CURRENT_DATE;
      
  3. Calculating age based on CURRENT_DATE in PostgreSQL:

    • Description: Calculates age using the current date and a birthdate column.
    • Code:
      SELECT name, birthdate, CURRENT_DATE - birthdate AS age
      FROM your_table;
      
  4. Formatting dates with CURRENT_DATE in PostgreSQL:

    • Description: Formats the current date as a string.
    • Code:
      SELECT TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD');
      
  5. Comparing dates using CURRENT_DATE in PostgreSQL:

    • Description: Compares date values using the current date.
    • Code:
      SELECT *
      FROM your_table
      WHERE date_column > CURRENT_DATE;
      
  6. Performing date arithmetic with CURRENT_DATE in PostgreSQL:

    • Description: Performs calculations involving the current date.
    • Code:
      SELECT CURRENT_DATE + INTERVAL '7 days';
      
  7. Handling time zones with CURRENT_DATE in PostgreSQL:

    • Description: Considers time zones in date operations.
    • Code:
      SET TIME ZONE 'UTC';
      SELECT CURRENT_DATE AT TIME ZONE 'America/New_York';
      
  8. Combining CURRENT_DATE with other date functions in PostgreSQL:

    • Description: Utilizes other date functions in conjunction with the current date.
    • Code:
      SELECT CURRENT_DATE, EXTRACT(MONTH FROM CURRENT_DATE), AGE('2000-01-01'::DATE, CURRENT_DATE);