How the CURRENT_DATE() function works in Mariadb?

The MariaDB CURRENT_DATE() function is used to retrieve the current date as a value in ‘YYYY-MM-DD’ or ‘YYYYMMDD’ format, depending on whether the function is used in a string or numeric context.

Posted on

The MariaDB CURRENT_DATE() function is used to retrieve the current date as a value in ‘YYYY-MM-DD’ or ‘YYYYMMDD’ format, depending on whether the function is used in a string or numeric context. This function is particularly useful for recording timestamps in data entries, generating reports with date stamps, and making date-based calculations or comparisons.

Syntax

The syntax for the CURRENT_DATE() function in MariaDB is as follows:

CURRENT_DATE()

This function does not take any arguments and returns the current date.

Examples

Retrieving the Current Date

To simply retrieve the current date, you can use the CURRENT_DATE() function in a SELECT statement:

SELECT CURRENT_DATE();

The output will be:

+----------------+
| CURRENT_DATE() |
+----------------+
| 2024-03-17     |
+----------------+

This statement will output the current date.

Comparing Dates

You can use the CURRENT_DATE() function to compare the current date with another date:

SELECT '2024-03-20' > CURRENT_DATE() AS 'Is Future Date?';

The output will be:

+-----------------+
| Is Future Date? |
+-----------------+
|               1 |
+-----------------+

This will return 1 (true) if the specified date is in the future compared to the current date.

Calculating Age

To calculate the age from a birthdate, you can use the CURRENT_DATE() function:

SELECT YEAR(CURRENT_DATE()) - YEAR('1990-05-25') AS 'Age';

The output will be:

+------+
| Age  |
+------+
|   34 |
+------+

This will return the age by subtracting the birth year from the current year.

Checking for a Specific Date

To check if today’s date is a specific date, you can use the CURRENT_DATE() function:

SELECT CURRENT_DATE() = '2024-12-25' AS 'Is Christmas?';

The output will be:

+---------------+
| Is Christmas? |
+---------------+
|             0 |
+---------------+

This will return 0 (false) if today’s date is not Christmas.

Using CURRENT_DATE() in Table Queries

If you need to use CURRENT_DATE() to filter results from a table, you would first create the table and insert some sample data:

DROP TABLE IF EXISTS events;
CREATE TABLE events (
    event_name VARCHAR(50),
    event_date DATE
);

INSERT INTO events (event_name, event_date) VALUES ('Conference', '2024-03-17'), ('Meeting', '2024-03-18');

Then, you can query to find events happening today:

SELECT event_name FROM events WHERE event_date = CURRENT_DATE();

The output will be:

+------------+
| event_name |
+------------+
| Conference |
+------------+

This will return the names of events occurring on the current date.

Here are a few functions related to the MariaDB CURRENT_DATE() function:

  • MariaDB CURDATE() function is an alias for CURRENT_DATE().
  • MariaDB NOW() function returns the current date and time.
  • MariaDB CURRENT_TIMESTAMP() function is used to return the current date and time as a timestamp.
  • MariaDB DATE() function is used to extract the date part from a datetime expression.

Conclusion

The CURRENT_DATE() function in MariaDB is a simple yet powerful tool for working with dates. It allows for easy retrieval of the current date and can be used in various scenarios, such as comparing dates, calculating ages, or filtering data based on the current date. Understanding how to use this function effectively can greatly enhance the functionality of your database queries and applications.