MySQL numeric functions
MySQL string functions
MySQL Date/Time functions
MySQL aggregate functions
MySQL flow control functions
The CURDATE()
and CURRENT_DATE()
functions in MySQL are used to return the current date. Both functions do not require any arguments and they both return the current date in 'YYYY-MM-DD' format.
Prerequisites:
Tutorial:
To start the mysql
command-line client, open your terminal or command prompt, and enter:
mysql -u [username] -p
Replace [username]
with your MySQL username and enter your password when prompted.
The basic syntax for using the CURDATE()
function is as follows:
SELECT CURDATE();
This will return the current date.
Similarly, you can use the CURRENT_DATE()
function to get the current date:
SELECT CURRENT_DATE();
This will also return the current date.
Both CURDATE()
and CURRENT_DATE()
functions will give you the same result.
Suppose you have a table named orders
with a column named order_date
. You can use the CURDATE()
or CURRENT_DATE()
function to find all orders placed on the current date like this:
SELECT * FROM orders WHERE order_date = CURDATE();
Or
SELECT * FROM orders WHERE order_date = CURRENT_DATE();
Both these queries will return a list of orders placed on the current date.
EXIT;
By using the CURDATE()
or CURRENT_DATE()
function in MySQL, you can easily get the current date. These functions can be useful for finding records associated with the current date, among other things.
How to Use CURDATE in MySQL:
CURDATE
function is used to retrieve the current date in MySQL.SELECT CURDATE(); -- Output: Current date in 'YYYY-MM-DD' format
How to Use CURRENT_DATE in MySQL:
CURRENT_DATE
is an alternative to CURDATE
for fetching the current date.SELECT CURRENT_DATE();
Getting the Current Date in MySQL with CURDATE:
CURDATE
.SELECT CURDATE() AS today;
Getting the Current Date in MySQL with CURRENT_DATE:
CURRENT_DATE
.SELECT CURRENT_DATE() AS today;
MySQL CURDATE vs CURRENT_DATE Comparison:
SELECT CURDATE() AS curdate_example, CURRENT_DATE() AS current_date_example;
Using CURDATE in SELECT Queries in MySQL:
CURDATE
in your SELECT queries.SELECT user_id, username, registration_date FROM users WHERE registration_date = CURDATE();
Using CURRENT_DATE in WHERE Clause in MySQL:
CURRENT_DATE
for date-based WHERE conditions.SELECT order_id, order_date FROM orders WHERE order_date >= CURRENT_DATE() - INTERVAL 7 DAY;
Formatting the Current Date in MySQL:
DATE_FORMAT
.SELECT DATE_FORMAT(CURDATE(), '%W, %M %e, %Y') AS formatted_date;
MySQL CURDATE and Time Zone Considerations:
CURDATE
.SET time_zone = '+00:00'; SELECT CURDATE();
Performing Date Calculations with CURDATE in MySQL:
CURDATE
.SELECT CURDATE() + INTERVAL 1 MONTH AS next_month;
Using CURDATE in MySQL Date Comparisons:
CURDATE
.SELECT user_id, username FROM users WHERE last_login_date < CURDATE() - INTERVAL 30 DAY;