Home > Database > Mysql Tutorial > body text

How to Extract Only the Date Component from a DATETIME Field in MySQL?

Patricia Arquette
Release: 2024-11-04 20:17:01
Original
844 people have browsed it

How to Extract Only the Date Component from a DATETIME Field in MySQL?

How to Isolate the Date Component from a DATETIME Field in MySQL

When dealing with DATETIME fields in MySQL, there are instances where extracting only the date component becomes necessary. Whether for data analysis, reporting, or filtering purposes, isolating the date can be crucial.

Question:

How can I perform a SELECT query in MySQL that retrieves only the date portion from a DATETIME field, disregarding the time component, even if the column is defined as DATETIME?

Answer:

To achieve this, the DATE() function in MySQL comes into play. By utilizing this function, you can effectively extract the date component from a DATETIME field, excluding any time information.

Syntax:

SELECT DATE(ColumnName) FROM TableName;
Copy after login

Example:

Consider a DATETIME field named "timestamp" in a table called "events." To select only the date component from this field, you would use the following query:

SELECT DATE(timestamp) FROM events;
Copy after login

Output:

This query would return results that include only the date portion of the "timestamp" column, in the following format:

yyyy-mm-dd
Copy after login

Additional Notes:

The MySQL DATE() function converts a DATETIME or TIMESTAMP value to a date value, discarding any time information. This is particularly useful when working with historical data or when comparing dates across different time zones.

The above is the detailed content of How to Extract Only the Date Component from a DATETIME Field 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