Often in PostgreSQL you may want to select rows where a date is equal to today.
You can use the following basic syntax to do so:
SELECT *
FROM emp
WHERE start_date = CURRENT_DATE;
This particular example will select all rows from the table named emp where the date in the start_date column is equal to today.
Note: In PostgreSQL, the command CURRENT_DATE returns the current date.
The following example shows how to use this syntax in practice.
Example: How to Select Rows Where Date is Today in PostgreSQL
Suppose that we create the following table named emp that contains information about various employees at some company including their starting date and their total sales made:
-- create table named 'emp'
CREATE TABLE emp (
empID INT PRIMARY KEY,
start_date DATE,
sales INT
);
-- add rows to the table
INSERT INTO emp VALUES (0001, '2024-08-26', 134);
INSERT INTO emp VALUES (0002, '2024-07-01', 201);
INSERT INTO emp VALUES (0003, '2024-08-27', 155);
INSERT INTO emp VALUES (0004, '2024-07-01', 98);
INSERT INTO emp VALUES (0005, '2024-08-23', 65);
INSERT INTO emp VALUES (0006, '2024-08-27', 52);
INSERT INTO emp VALUES (0007, '2024-08-27', 50);
-- view all rows in table
SELECT * FROM emp;
empid | start_date | sales
-------+------------+-------
1 | 2024-08-26 | 134
2 | 2024-07-01 | 201
3 | 2024-08-27 | 155
4 | 2024-07-01 | 98
5 | 2024-08-23 | 65
6 | 2024-08-27 | 52
7 | 2024-08-27 | 50
This article is currently being written on 8/27/2024.
We can use the following syntax to select all rows where the date in the start_date column is equal to today.
SELECT *
FROM emp
WHERE start_date = CURRENT_DATE;
empid | start_date | sales
-------+------------+-------
3 | 2024-08-27 | 155
6 | 2024-08-27 | 52
7 | 2024-08-27 | 50
Notice that this returns three rows from the table.
We can see that each of the dates in the start_date column of the resulting table are indeed equal to 8/27/2024.
Feel free to use similar syntax to filter for rows where a date column is equal to the current date in your own table.
Additional Resources
The following tutorials explain how to perform other common tasks in PostgreSQL:
PostgreSQL: How to Select Rows in Date Range
PostgreSQL: Select Rows Where Date is Greater Than
PostgreSQL: Calculate Difference Between Dates in Days
