In MariaDB, CURDATE()
is a built-in date and time function that returns the current date.
The date is returned in either 'YYYY-MM-DD'
or YYYYMMDD
, depending on whether the function is being used in a string or numeric context.
Syntax
The syntax goes like this:
CURDATE()
No arguments are required or accepted.
It also has the following synonyms:
CURRENT_DATE
CURRENT_DATE()
Example
Here’s an example:
SELECT CURDATE();
Result:
+------------+ | CURDATE() | +------------+ | 2021-05-08 | +------------+
Numeric Context
When CURDATE()
is used in a numeric context, the resulting date is in YYYYMMDD
format.
Example:
SELECT CURDATE() + 0;
Result:
+---------------+ | CURDATE() + 0 | +---------------+ | 20210508 | +---------------+
Synonyms
CURRENT_DATE
and CURRENT_DATE()
are synonyms for CURDATE()
. Therefore, we can use either one to get the same result.
Example
SELECT
CURDATE(),
CURRENT_DATE,
CURRENT_DATE();
Result:
+------------+--------------+----------------+ | CURDATE() | CURRENT_DATE | CURRENT_DATE() | +------------+--------------+----------------+ | 2021-05-08 | 2021-05-08 | 2021-05-08 | +------------+--------------+----------------+
Adding to the Current Date
There are many ways to perform arithmetic on dates in MariaDB. You can use such methods to add a number of days, weeks, months, or years to the current date.
Here’s an example of using the addition operator (+
) to add 6 months to the date:
SELECT
CURDATE(),
CURDATE() + INTERVAL 6 MONTH;
Result:
+------------+------------------------------+ | CURDATE() | CURDATE() + INTERVAL 6 MONTH | +------------+------------------------------+ | 2021-05-08 | 2021-11-08 | +------------+------------------------------+
Also see functions like DATE_ADD()
and ADDDATE()
for an alternative way to add to the current date.
Subtracting from the Current Date
Here’s an example of using the subtraction operator (-
) to subtract 6 months from the current date:
SELECT
CURDATE(),
CURDATE() - INTERVAL 6 MONTH;
Result:
+------------+------------------------------+ | CURDATE() | CURDATE() + INTERVAL 6 MONTH | +------------+------------------------------+ | 2021-05-08 | 2021-11-08 | +------------+------------------------------+
See functions like DATE_SUB()
and SUBDATE()
for an alternative way to add to the current date.
No Arguments
The CURDATE()
function doesn’t accept any arguments.
Here’s what happens when we pass an argument:
SELECT CURDATE(1);
Result:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '1)' at line 1