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
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()
.
Retrieving the current date with CURRENT_DATE in PostgreSQL:
SELECT CURRENT_DATE;
Using CURRENT_DATE in WHERE clauses in PostgreSQL:
SELECT * FROM your_table WHERE date_column = CURRENT_DATE;
Calculating age based on CURRENT_DATE in PostgreSQL:
SELECT name, birthdate, CURRENT_DATE - birthdate AS age FROM your_table;
Formatting dates with CURRENT_DATE in PostgreSQL:
SELECT TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD');
Comparing dates using CURRENT_DATE in PostgreSQL:
SELECT * FROM your_table WHERE date_column > CURRENT_DATE;
Performing date arithmetic with CURRENT_DATE in PostgreSQL:
SELECT CURRENT_DATE + INTERVAL '7 days';
Handling time zones with CURRENT_DATE in PostgreSQL:
SET TIME ZONE 'UTC'; SELECT CURRENT_DATE AT TIME ZONE 'America/New_York';
Combining CURRENT_DATE with other date functions in PostgreSQL:
SELECT CURRENT_DATE, EXTRACT(MONTH FROM CURRENT_DATE), AGE('2000-01-01'::DATE, CURRENT_DATE);