The MySQL Date Function is very useful to extract the datepart from a given date or DateTime expression. Let us see how to use this function to get the date from an expression with an example, and the basic syntax of this is as shown below:
DATE(date or DateTime expression);
MySQL Date function Example
The following query shows you the basic use of this method. Here, we are extracting the datepart from the date expression and the DateTime expression.
SELECT DATE('2017-10-25 01:05:22');
Invalid Expressions as Input Example 2
The following query show what happens when we try to extract the datepart from an invalid expression. I mean, here, we are trying to extract the date part from an invalid date query.
From the above screenshot, it returned NULL. Let me try another example. Here, we are trying to extract the datepart from a string. That’s why it returns NULL.
SELECT DATE(NOW() + 2);
In this instance, we show you how to use this method on a table. Here, we are extracting the Datepart from the HireDate 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 customer;