Languages
[Edit]
EN

MySQL - compare datetime values

0 points
Created by:
martineau
1350

In this article, we would like to show you how to compare datetime values in MySQL.

Quick solution:

Notes:

  1. you can replace the <= operator with any other comparison operator,
  2. to see how to use BETWEEN keyword with dates check this article.

 

Practical example

Edit

To show how to compare datetime values, we will use the following table:

MySQL - example data used to compare date values
MySQL - example data used to compare datetime values

Note:

At the end of this article you can find database preparation SQL queries.

Example 1

Edit

In this example, we will display all the datetimes before 2000-01-04.

Query:

Output:

MySQL - compare datetime values - result
MySQL - compare datetime values - result

Example 2

Edit

In this example, we will select all the datetimes from 2000-01-04 inclusive and display them without time using DATE() function (yyyy-mm-dd format only).

Query:

Output:

MySQL - compare datetime values, display without time - result
MySQL - compare datetime values, display without time - result

Database preparation

Edit

create_tables.sql file:

insert_data.sql file:

Alternative titles

  1. MySQL - compare two datetimes
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MySQL - dates

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join