Home > Database > Mysql Tutorial > body text

How to Compare a DATE String with a DATETIME Field in MySQL?

Linda Hamilton
Release: 2024-11-12 11:29:02
Original
880 people have browsed it

How to Compare a DATE String with a DATETIME Field in MySQL?

Comparing DATE String with DATETIME Field in MySQL

It is indeed possible to compare a DATE string, such as "2010-04-29," with strings stored as DATETIME in MySQL. To achieve this, you can use the DATE() function.

The DATE() function extracts the date portion from a DATETIME field, discarding the time component. By using this function, you can compare the requested DATE string directly with the DATETIME field.

The following SQL query demonstrates how to accomplish this:

SELECT * FROM `calendar` WHERE DATE(startTime) = '2010-04-29'
Copy after login

This query will retrieve all rows from the calendar table where the date portion of the startTime field matches "2010-04-29." You will now be able to retrieve data accurately for entries in the table, even though they are stored as DATETIME.

The above is the detailed content of How to Compare a DATE String with 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