MySQL Date function is one of the Date Function, which is used to extract the date part from a given date or DateTime expression.
In this article, we will show you, How to use this MySQL Date function to get the date from an expression with example.
MySQL Date Syntax
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 will show 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');
Let us see another example. Here, we are extracting date part from the current date and time returned by the Now() function.
MySQL date Function Example 2
The following query will show you, what happens when we try to extract the date part from an invalid expression. I mean, here, we are trying to extract date part from an invalid 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);
MySQL Date Example 3
In this example, we will show you, how to use this Date function on a table. Here, we are extracting 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;
Thank You for Visiting Our Blog