The CURDATE() function in MySQL is used to return the current date in the YYYY-MM-DD format, without the time part. It's often used when you need to work with today's date or compare dates in queries.
Syntax:
CURDATE()
Example 1: Get the current date
SELECT CURDATE();
Output:
2024-12-23
Example 2: Using CURDATE() in a query
You can use CURDATE() to filter records or calculate date-based values.
Example 2a: Get rows where the order_date is today's date
SELECT * FROM orders
WHERE order_date = CURDATE();
Example 2b: Get records from the past 7 days
SELECT * FROM orders
WHERE order_date >= CURDATE() - INTERVAL 7 DAY;
Example 3: Compare dates
SELECT * FROM events
WHERE event_date > CURDATE();
Notes:
CURDATE()only returns the date in theYYYY-MM-DDformat, and does not include time. For the current date and time, you should use theNOW()function instead.- If you need the date in a different format, you can use
DATE_FORMAT()to adjust the output.
Example of using DATE_FORMAT() with CURDATE():
SELECT DATE_FORMAT(CURDATE(), '%d-%m-%Y') AS formatted_date;
Output:
23-12-2024
In summary, CURDATE() is a simple and effective way to retrieve today's date for use in queries or calculations.
No comments:
Post a Comment