Home >Database >Mysql Tutorial >How Can I Extract Only the Date from a DATETIME Field in MySQL?

How Can I Extract Only the Date from a DATETIME Field in MySQL?

Barbara Streisand
Barbara StreisandOriginal
2024-11-07 10:35:03799browse

How Can I Extract Only the Date from a DATETIME Field in MySQL?

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 DATE() function returns a value in the 'YYYY-MM-DD' format.
  • For further exploration, refer to the MySQL documentation on the DATE() function.

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!

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