How to extract from datetime column in MySQL by comparing only date and ignoring whitespace?

MySQLMySQLi Database

To extract from datetime column, you can use date() along with trim(). Here, trim() is used to remove whitespace while comparing. Let us first create a table −

mysql> create table DemoTable661(Duedate datetime);
Query OK, 0 rows affected (0.57 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable661 values(' 2019-01-21 12:02:21');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable661 values(' 2019-07-11 11:55:59 ');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable661 values('2019-11-21 04:00:59 ');
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable661;

This will produce the following output −

+---------------------+
| Duedate             |
+---------------------+
| 2019-01-21 12:02:21 |
| 2019-07-11 11:55:59 |
| 2019-11-21 04:00:59 |
+---------------------+
3 rows in set (0.00 sec)

Following is the query to extract date from datetime column −

mysql> select *from DemoTable661 where trim(date(Duedate))='2019-07-11';

This will produce the following output −

+---------------------+
| Duedate             |
+---------------------+
| 2019-07-11 11:55:59 |
+---------------------+
1 row in set (0.00 sec)
raja
Published on 23-Aug-2019 12:30:25
Advertisements