MySQL Date Function

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 MySQL 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('2016-11-25');

SELECT DATE('2017-10-25 01:05:22');

Let us see another example of a method. Here, we are extracting the datepart from the current DateTime returned by the Now() function.

SELECT DATE(NOW());
MySQL Date Function 3

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 MySQL dt.

SELECT DATE('2016-02-31');
MySQL Date Function 4

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);
MySQL Date Function 5

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;
 
MySQL Date Function 6