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 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');
Example 1

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());
Example 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 date query.

SELECT DATE('2016-02-31');
Example 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);
Date Example 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

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.