Home >Database >Mysql Tutorial >How to extract dates from DATETIME fields in MySQL?
Retrieve Dates from DATETIME in MySQL: Selecting by Date Only
When working with DATETIME fields in MySQL, it becomes necessary to isolate dates from the timestamp. To achieve this, you can utilize MySQL's DATE() function. Here's how:
To select dates from a DATETIME field in MySQL, use the following syntax:
SELECT DATE(ColumnName) FROM tablename;
Where ColumnName represents the column containing the DATETIME values.
Example:
Let's say you have a table named "Events" with a column named "EventDateTime" containing DATETIME values. The following query will select only the dates from "EventDateTime" for each event:
SELECT DATE(EventDateTime) FROM Events;
Result:
The query will return a table with a single column showing only the dates, in the format 'YYYY-MM-DD'. For instance, if the "EventDateTime" value was '2012-01-23 09:24:41', the query would return '2012-01-23'.
Additional Information:
The MySQL DATE() function removes the time component from DATETIME values. It does not modify the original values in the table. For more information on the DATE() function, refer to the official MySQL documentation.
The above is the detailed content of How to extract dates from DATETIME fields in MySQL?. For more information, please follow other related articles on the PHP Chinese website!