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 in MySQL 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('2017-10-25 01:05:22');
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.
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.
FROM `MySQL Tutorial`.customer;