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
Release: 2024-11-07 10:35:03
Original
776 people have browsed it

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

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

To extract the date from this field while excluding the time, use the following query:

SELECT DATE(DateTimeColumn) FROM ExampleTable;
Copy after login

The result of the query will be:

2012-01-23
Copy after login

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!

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