Summary: in this tutorial, you will learn how to use the SQL CURRENT_DATE
function to get the current date.
Introduction to the SQL CURRENT_DATE function #
The CURRENT_DATE
function returns the current date of the machine where the database server is running.
Here’s the syntax of the CURRENT_DATE
function:
CURRENT_DATE
Code language: SQL (Structured Query Language) (sql)
Note that the CURRENT_DATE
function does not require parentheses because it is a reversed word in SQL.
The CURRENT_DATE
function returns a value of DATE
type without the time component.
In practice, you’ll find the CURRENT_DATE
function helpful for comparing and filtering dates with the current date.
SQL CURRENT_DATE function examples #
Let’s take some examples of using the CURRENT_DATE
function.
Retrieving the current date #
The following statement uses the CURRENT_DATE
function to retrieve the current date:
SELECT CURRENT_DATE AS today;
Code language: SQL (Structured Query Language) (sql)
Here’s the output when we run the statement on January 20, 2025
:
today
------------
2025-01-20
Code language: SQL (Structured Query Language) (sql)
The output indicates the format of the date is YYYY-MM-DD
, which is ISO standard format for a date.
Using the CURRENT_DATE function as the default value #
When creating a new table or adding a column to an existing table, you can use the CURRENT_DATE
function as a default value for a column. For example:
First, create a new table called salary_changes
that uses the CURRENT_DATE
function as a default value for a column:
CREATE TABLE salary_changes (
employee_id INT NOT NULL,
changed_date DATE NOT NULL DEFAULT CURRENT_DATE,
salary DEC NOT NULL,
PRIMARY KEY (employee_id, changed_date)
);
Code language: SQL (Structured Query Language) (sql)
Second, insert a new row into the salary_changes
table:
INSERT INTO
salary_changes (employee_id, salary)
VALUES
(100, 25000);
Code language: SQL (Structured Query Language) (sql)
When you insert a new row without providing a value for the changed_date
column, the database system uses the current date as the value for the column.
Third, retrieve data from the salary_changes
table:
SELECT * FROM salary_changes;
Code language: SQL (Structured Query Language) (sql)
Here’s the output when we run the insert statement on January 20, 2025
:
employee_id | changed_date | salary
-------------+--------------+--------
100 | 2025-01-20 | 25000
Code language: SQL (Structured Query Language) (sql)
Comparing with other dates #
You can use the CURRENT_DATE
function to compare with other dates.
For example, the following statement finds the employees who have salary changes before today:
SELECT
first_name,
last_name,
s.salary new_salary
FROM
salary_changes s
INNER JOIN employees e ON e.employee_id = s.employee_id
WHERE
changed_date < CURRENT_DATE;
Code language: SQL (Structured Query Language) (sql)
Adding or subtracting days from the current date #
You can add or subtract an interval from the current date. For example:
SELECT CURRENT_DATE + INTERVAL '7 DAY' AS next_week,
CURRENT_DATE - INTERVAL '7 DAY' AS last_week;
Code language: SQL (Structured Query Language) (sql)
Getting the current date in Oracle Database and SQL Server #
Oracle Database’s CURRENT_DATE
function returns a value that includes both date and time. To get the current date without time, you use the TRUNC
function to truncate the time part:
SELECT
TRUNC(CURRENT_DATE)
FROM
dual;
Code language: SQL (Structured Query Language) (sql)
SQL Server does not support CURRENT_DATE
function. However, it offers a function called GETDATE()
that returns the current date and time.
To get the current date, you use the CAST()
function with the GETDATE()
function as shown in the following statement:
SELECT CAST(GETDATE() AS DATE) 'Current Date';
Code language: SQL (Structured Query Language) (sql)
Summary #
- Use the SQL
CURRENT_DATE
function to return the current date in the formatYYYY-MM-DD
.