DATE() in MySQL
Last Updated :
11 Sep, 2024
Improve
The DATE()
function in MySQL is designed to extract the date portion (year, month, and day) from a given date, datetime or timestamp value. In this article, We will learn about DATE() in MySQL by understanding their examples.
DATE() in MySQL
- In MySQL the
DATE()
function is used to extract the date part (year, month, and day) from a given date, datetime or timestamp value. - This function helps to remove the time component from datetime values and leaves only the date which is useful in various database operations where the time part is unnecessary.
Syntax:
DATE(expression)
expression: This can be a date, datetime, or timestamp value from which you want to extract the date.
Examples of DATE()
Below are some common examples or usages of the DATE() function :
Example 1: Extracting date from a given date.
SELECT DATE('2024-09-11 12:34:56');
Output:
2024-09-11
Example 2: Extracting date from a given DateTime.
Select DATE("2020-06-23 10:34:29");
Output:
2020-06-23
Example 3: Extracting date from a given date.
SELECT DATE("The date is 2020-06-23");
Output:
NULL
Conclusion
The DATE() function in MySQL is a crucial tool for extracting the date portion from datetime or timestamp values. It simplifies queries where only the date component is relevant, and it gracefully handles invalid formats by returning NULL
.