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

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

DDD
Release: 2024-11-04 19:53:01
Original
692 people have browsed it

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

Selecting Date from DATETIME Fields in MySQL

When dealing with DATETIME fields in MySQL, it may be necessary to retrieve only the date portion, excluding the time component. To achieve this, MySQL provides the DATE() function.

Syntax:

SELECT DATE(ColumnName) FROM tablename;

Example:

Consider the following MySQL table:

<code class="sql">CREATE TABLE example (
    id INT AUTO_INCREMENT,
    timestamp DATETIME,
    ...
);</code>
Copy after login

To select the date from the timestamp column, you can use the following query:

<code class="sql">SELECT DATE(timestamp) FROM example;</code>
Copy after login

Output:

+---------------------+
| DATE(timestamp)      |
+---------------------+
| 2023-03-08           |
| 2023-03-09           |
| 2023-03-10           |
+---------------------+
Copy after login

As you can see, the query returns the date only, without the time component.

Note:

  • The DATE() function can also be used to extract the date from other DATETIME-related data types, such as TIMESTAMP and TIMESTAMP WITH TIME ZONE.
  • For more information on the DATE() function, refer to the MySQL documentation.

The above is the detailed content of How to 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template