- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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)
- Related Articles
- Display distinct dates in MySQL from a column with date records
- MySQL query to fetch the latest date from a table with date records
- MySQL queries to update date records with NULL values
- How to change the date in a table with date records with MySQL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Find the difference between current date and the date records from a MySQL table
- Fetching records from a table with NULL and other values on the basis of conditions in MySQL
- Select the date records between two dates in MySQL
- Add 30 days to date in a MySQL table with arrival date records
- Select and filter the records on month basis in a MySQL table?
- Display month names and year from a column with date records with MySQL
- Display selected records from a MySQL table with IN() operator
- How to compare the first date and the last date from a MySQL column with date records?
- How to find last date from records with date values in MySQL?
- Create a temporary table with dates in MySQL

Advertisements