MySQL DATE() Function
The DATE()
function in MySQL extracts the date portion from a datetime or date value. This is a handy function for isolating the date part from a combined date and time value or for formatting data for display.
DATE(): Definition and Usage
DATE()
is frequently used to separate the date component from a combined datetime value or to perform operations involving only the date part of a timestamp. If the input expression isn't a valid date or datetime, it returns NULL
.
Syntax
Syntax
DATE(expression)
Parameter Values
Parameter | Description |
---|---|
expression |
A valid date or datetime value. This is required. If the expression isn't a valid date or datetime, the function returns NULL . |
Examples
Extracting the Date Part from a DATE Value
This example shows extracting the date part from a date.
Syntax
SELECT DATE("2017-06-15");
Output
2017-06-15
Extracting the Date Part from a DATETIME Value
Extracting the date from a datetime value.
Syntax
SELECT DATE("2017-06-15 09:34:21");
Output
2017-06-15
Handling a Non-Date Input
If the input isn't a valid date, you get NULL
.
Syntax
SELECT DATE("The date is 2017-06-15");
Output
NULL
Extracting the Date from a Table Column
Extracting the date part from the 'OrderDate' column of the 'Orders' table (assuming an 'Orders' table exists with an 'OrderDate' column).
Syntax
SELECT DATE(OrderDate) FROM Orders;
Output
(The date part of each OrderDate from the Orders table will be shown here.)