PostgreSQL – CURRENT_DATE Function

In PostgreSQL, the CURRENT_DATE function is used to retrieve the current date without the time component. It returns the current date in the date data type, which includes the year, month, and day information. This function is helpful when you need to work with the current date in queries or when inserting records with the current date into a table.

The basic syntax of the CURRENT_DATE function is as shown below:

SELECT CURRENT_DATE;

Below is an example of using the CURRENT_DATE function:

SELECT CURRENT_DATE AS current_date;

This query will return the current date in the format YYYY-MM-DD, such as 2023-08-06.

You can use the CURRENT_DATE function in various scenarios, such as:

  1. Inserting the current date into a table:
INSERT INTO your_table (column_name, date_column)
VALUES ('some_value', CURRENT_DATE);

In this example, your_table is the name of your table, column_name is the name of the column where you want to insert some value, and date_column is the column where you want to store the current date.

  1. Comparing dates:
SELECT *
FROM your_table
WHERE date_column >= CURRENT_DATE;

This query will retrieve records from your_table where the date_column is greater than or equal to the current date.

  1. Calculating date differences:
SELECT '2023-12-25' - CURRENT_DATE AS days_until_christmas;

This query will calculate the number of days until Christmas from the current date.

Since CURRENT_DATE does not include the time component, it is useful when you only need the date part for your calculations or comparisons. If you require the current date and time together, you should use the NOW() function.

Similar Posts