Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting data between given date range by comparing two dates in a table

I have a table trans and it has columns like tranno, start_date, stop_date. Data is as follows.

tranno, start_date, stop_date
123, 07-jul-2019, 06-jun-2020
124, 07-jul-2020, 06-jun-2021
125, 07-jul-2021, 06-jun-2022
126, 07-jul-2022, 06-jun-2023

My requirement is , I want to get trans between given date range. Lets say i want trans between 08-jul-2020 and 10-aug-2022. Then my output should be like

Tranno
124
125
126

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming your start_date and stop_date columns are bona fide date columns, then you may use the overlapping date range formula here:

SELECT *
FROM yourTable
WHERE stop_date  >= '2020-07-08'::date AND
      start_date <= '2022-08-10'::date;

If you are literally storing text dates as e.g. 07-jul-2019, then the best thing to do would be to change your table design and store proper dates.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading