How to compare Year, Month and Day in a MySQL query and display matching records

MySQLMySQLi Database

For this, you can use DATE(). Let us first create a table −

mysql> create table DemoTable864(DueDateTime timestamp);
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable864 values('2019-01-10 12 −34 −55');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable864 values('2016-12-11 11 −12 −00');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable864 values('2015-04-01 10 −00 −00');
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable864 values('2017-05-20 04 −40 −10');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable864;

This will produce the following output −

+---------------------+
| DueDateTime         |
+---------------------+
|2019-01-10 12 −34 −55|
|2016-12-11 11 −12 −00|
|2015-04-01 10 −00 −00|
|2017-05-20 04 −40 −10|
+---------------------+
4 rows in set (0.00 sec)

Following is the query to compare year, month and day −

mysql> select *from DemoTable864 where date(DueDateTime)='2015-04-01';

This will produce the following output −

+-----------------------+
| DueDateTime           |
+-----------------------+
| 2015-04-01 10 −00 −00 |
+-----------------------+
1 row in set (0.04 sec)
raja
Published on 03-Sep-2019 12:58:07
Advertisements