MySQL MariaDB

How to Use CURRENT_DATE() Function in MySQL?

MySQL provides a wide range of functions that can be utilized to store and manipulate the date values, such as the CURRENT_DATE() function. This function returns the current date value, which can be used for a variety of purposes such as filtering the table’s data based on the current date, calculating the age of the person, etc.

This article will guide you on how to use CURRENT_DATE() function in MySQL.

How to Use CURRENT_DATE() Function in MySQL?

The “CURRENT_DATE()” is a function in MySQL that returns the current date value. The stated function can be used for different purposes, such as retrieving the current date, inserting the current date into a table, etc.

Let’s move to the example to understand the use cases of the CURRENT_DATE() function.

Example 1: Retrieving the Current Date

The current date value can be retrieved by using the “SELECT” statement with the “CURRENT_DATE()” function as given below:

SELECT CURRENT_DATE();

 

Output

The output showed the current date value.

Example 2: Formatting Dates

The current date can be retrieved in a specific format by using the “SELECT” statement with the “DATE_FORMAT()” function. For this, pass the “CURRENT_DATE()” function and a format string as arguments to the DATE_FORMAT() function, as shown below:

SELECT DATE_FORMAT(CURRENT_DATE(), '%M %d, %Y');

 

Output

The output displayed the current date value in a proper format.

Example 3: Filtering Records by Current Date

The “CURRENT_DATE()” function can be utilized to filter the specific tables data based on today’s date, as shown below:

SELECT *
FROM orders
WHERE order_date = CURRENT_DATE();

 

In the above example, the “orders” table is filtered based on the current date.

Output

The output depicts the filtered data.

Example 4: Calculating the Age of Persons

The CURRENT_DATE() function can be utilized to calculate the age of any person by using their date of birth. The example of calculating the age of persons in the “persons_details” table is given below:

SELECT name, YEAR(CURRENT_DATE()) - YEAR(date_of_birth)
AS age FROM persons_details;

 

Output

The output showed the age of persons from the “persons_details” table.

Example 5: Inserting the Current Date into a Table

The CURRENT_DATE() function can be used to insert the current date value into a particular table. Here is an example of inserting the different value and current date into the “orders” table:

INSERT INTO orders ( id, user_id, product_id, quantity, order_date)
VALUES (22, 46, 10, 2, CURRENT_DATE());

 

In the above example, “INSERT INTO” is used to insert today’s date into the orders table.

Output

The output showed that the current date had been inserted.

Conclusion

The CURRENT_DATE() function in MySQL is a powerful tool that can be used for various purposes such as retrieving the current date, filtering records by the current date, formatting dates, calculating the age of persons, and so on. By using this function, it becomes easier to manage and manipulate data related to date values in MySQL databases. This guide provides detailed information with examples of how to use the CURRENT_DATE() function.

About the author

Danish Ghafoor

I am a computer science graduate with a certification in cybersecurity. My passion for technology has led me to develop a deep understanding of the challenges and solutions in the field of information security. I am dedicated to staying up-to-date with the latest industry trends and technologies, and constantly seeking out new opportunities to expand my knowledge and skills.