Home >Database >Mysql Tutorial >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>
To select the date from the timestamp column, you can use the following query:
<code class="sql">SELECT DATE(timestamp) FROM example;</code>
Output:
+---------------------+ | DATE(timestamp) | +---------------------+ | 2023-03-08 | | 2023-03-09 | | 2023-03-10 | +---------------------+
As you can see, the query returns the date only, without the time component.
Note:
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!