Home >Database >Mysql Tutorial >How to extract dates from DATETIME fields in MySQL?

How to extract dates from DATETIME fields in MySQL?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-05 04:55:02861browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn