The MySQL Date function is one of the Date Functions, which extracts the date part from a given date or DateTime expression. Let us see how to use this MySQL Date function to get the date from an expression with example. The basic syntax of the Date Function is as shown below:
DATE(date or DateTime expression);
MySQL Date function Example
The following query shows you the basic use of this Date function. Here, we are extracting the date part from the date expression and the DateTime expression.
SELECT DATE('2016-11-25');
SELECT DATE('2017-10-25 01:05:22');
Let us see another Date Function example. Here, we are extracting the date part from the current date and time returned by the Now() function.
SELECT DATE(NOW());
MySQL date Function Example 2
The following query show what happens when we try to extract the date part from an invalid expression. I mean, here, we are trying to extract the date part from an invalid MySQL date.
SELECT DATE('2016-02-31');
From the above screenshot, it returned NULL. Let me try another example. Here, we are trying to extract the date part from a string. That’s why it returns NULL.
SELECT DATE(NOW() + 2);
Date Function Example 3
In this instance, we show you how to use this Date function on a table. Here, we are extracting the Date part from the Hire Date column. For this MySQL date function demonstration, we are using Workbench to write a query against the customer database.
SELECT EmpID,
FirstName,
LastName,
Occupation,
YearlyIncome,
Sales,
HireDate,
DATE(HireDate)
FROM `MySQL Tutorial`.customer;