Retrieving Dates from DATETIME Fields in MySQL
When working with MySQL databases, it may be necessary to select only date information from fields that contain both date and time values. In this scenario, where a DATETIME field holds the desired data, the following approach can be used.
DATE() Function
To bypass the time component and select only the date portion of a DATETIME field, MySQL provides the DATE() function. Its syntax is straightforward:
DATE(ColumnName)
where "ColumnName" represents the target DATETIME field.
Example
Suppose a table named "ExampleTable" contains a DATETIME field called "DateTimeColumn" with the following value:
2012-01-23 09:24:41
To extract the date from this field while excluding the time, use the following query:
SELECT DATE(DateTimeColumn) FROM ExampleTable;
The result of the query will be:
2012-01-23
Additional Notes
The above is the detailed content of How Can I Extract Only the Date from a DATETIME Field in MySQL?. For more information, please follow other related articles on the PHP Chinese website!