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

How to extract dates from DATETIME fields in MySQL?

Patricia Arquette
Release: 2024-11-05 04:55:02
Original
876 people have browsed it

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;
Copy after login

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;
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template