EN
MySQL - compare datetime values
0 points
In this article, we would like to show you how to compare datetime values in MySQL.
Quick solution:
xxxxxxxxxx
1
SELECT *
2
FROM `table_name`
3
WHERE `datetime_column_name` <= 'yyyy-mm-dd';
Notes:
- you can replace the
<=
operator with any other comparison operator,- to see how to use
BETWEEN
keyword with dates check this article.
To show how to compare datetime values, we will use the following table:

Note:
At the end of this article you can find database preparation SQL queries.
In this example, we will display all the datetimes before 2000-01-04
.
Query:
xxxxxxxxxx
1
SELECT *
2
FROM `dates`
3
WHERE `date` < '2000-01-04';
Output:

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:
xxxxxxxxxx
1
SELECT DATE(`date`)
2
FROM `dates`
3
WHERE `date` <= '2000-01-04';
Output:

create_tables.sql
file:
xxxxxxxxxx
1
create TABLE `dates`(
2
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
3
`date` DATETIME,
4
PRIMARY KEY (`id`)
5
);
insert_data.sql
file:
xxxxxxxxxx
1
insert into `dates`
2
(`date`)
3
VALUES
4
('2000-01-01'),
5
('2000-01-02'),
6
('2000-01-03'),
7
('2000-01-04'),
8
('2000-01-05'),
9
('2000-01-06');