In MariaDB, DATE() is a built-in date and time function that extracts the date part of a date or datetime expression.
You pass the date/datetime expression as an argument when calling the function.
Syntax
The syntax goes like this:
DATE(expr)
Where expr is an the date/datetime expression.
Example
Here’s an example:
SELECT DATE('2057-08-25 18:00:10');
Result:
+-----------------------------+
| DATE('2057-08-25 18:00:10') |
+-----------------------------+
| 2057-08-25 |
+-----------------------------+
Here it is with just the date expression:
SELECT DATE('2057-08-25');
Result:
+--------------------+
| DATE('2057-08-25') |
+--------------------+
| 2057-08-25 |
+--------------------+
Current Date
Here, we return the current date by passing NOW() as the argument:
SELECT
NOW(),
DATE(NOW());
Result:
+---------------------+-------------+ | NOW() | DATE(NOW()) | +---------------------+-------------+ | 2021-05-10 09:26:32 | 2021-05-10 | +---------------------+-------------+
Invalid Argument
When passed an invalid argument, DATE() returns null:
SELECT DATE('Homer Simpson');
Result:
+-----------------------+
| DATE('Homer Simpson') |
+-----------------------+
| NULL |
+-----------------------+
Although, until MariaDB 5.5.32, some versions of MariaDB returned 0000-00-00 when passed an invalid date.
Missing Argument
Calling DATE() without passing an argument results in an error:
SELECT DATE();
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 ')' at line 1