How to compare DateTime Column with only Date not time in MySQL?


To compare DateTime column with only Date, you need to use the Date() method. Following is the syntax. Below, you need to date in the 'yourDateValue':

select *from yourTableName where Date(yourColumnName)='yourDateValue';

Let us first create a table:

mysql> create table DemoTable
(
   ArrivalTime datetime
);
Query OK, 0 rows affected (0.74 sec)

Following is the query to insert some records in the table using insert command:

mysql> insert into DemoTable values('2019-01-31 02:34:56');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values('2019-04-09 18:20:58');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('2019-05-11 19:45:23');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('2019-02-03 21:10:02');
Query OK, 1 row affected (0.16 sec)

Following is the query to display records from the table using select command:

mysql> select *from DemoTable;

This will produce the following output with date and time record:

+---------------------+
| ArrivalTime         |
+---------------------+
| 2019-01-31 02:34:56 |
| 2019-04-09 18:20:58 |
2019-05-11 19:45:23   |
| 2019-02-03 21:10:02 |
+---------------------+
4 rows in set (0.00 sec)

Following is the query to compare only date not time. Here, we are comparing the date:

mysql> select *from DemoTable where Date(ArrivalTime)='2019-04-09';

This will produce the following output:

+---------------------+
| ArrivalTime         |
+---------------------+
| 2019-04-09 18:20:58 |
+---------------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements