- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Filter dates from a table with DATE and NULL records in MySQL
Let us first create a table −
mysql> create table DemoTable ( FirstDate datetime, SecondDate datetime ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-01-21','2018-01-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-10-04','2019-08-14'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-05-01','2019-09-11'); Query OK, 1 row affected (0.65 sec) mysql> insert into DemoTable values(NULL,NULL); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-03-01',NULL); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
Display all records from the table using select statement:
This will produce the following output −
+---------------------+---------------------+ | FirstDate | SecondDate | +---------------------+---------------------+ | 2019-01-21 00:00:00 | 2018-01-21 00:00:00 | | 2019-10-04 00:00:00 | 2019-08-14 00:00:00 | | 2019-05-01 00:00:00 | 2019-09-11 00:00:00 | | NULL | NULL | | 2019-03-01 00:00:00 | NULL | +---------------------+---------------------+ 5 rows in set (0.00 sec)
Following is the query to filter dates from a table with DATE and NULL records −
mysql> select *from DemoTable where FirstDate <= curdate() and (SecondDate >=curdate() or SecondDate IS NULL);
This will produce the following output −
+---------------------+---------------------+ | FirstDate | SecondDate | +---------------------+---------------------+ | 2019-05-01 00:00:00 | 2019-09-11 00:00:00 | | 2019-03-01 00:00:00 | NULL | +---------------------+---------------------+ 2 rows in set (0.00 sec)
Advertisements